
<!DOCTYPE HTML>
<html lang="zh-hans" >
    <head>
        <meta charset="UTF-8">
        <meta content="text/html; charset=utf-8" http-equiv="Content-Type">
        <title>9.基于AQS的并发工具.md · 计算机 ,java , 大数据 基础知识体系</title>
        <meta http-equiv="X-UA-Compatible" content="IE=edge" />
        <meta name="description" content="">
        <meta name="generator" content="GitBook 3.2.3">
        <meta name="author" content="hzh">
        
        
    
    <link rel="stylesheet" href="../../gitbook/style.css">

    
            
                
                <link rel="stylesheet" href="../../gitbook/gitbook-plugin-splitter/splitter.css">
                
            
                
                <link rel="stylesheet" href="../../gitbook/gitbook-plugin-expandable-chapters-small/expandable-chapters-small.css">
                
            
                
                <link rel="stylesheet" href="../../gitbook/gitbook-plugin-anchors/plugin.css">
                
            
                
                <link rel="stylesheet" href="../../gitbook/gitbook-plugin-anchor-navigation-ex/style/plugin.css">
                
            
                
                <link rel="stylesheet" href="../../gitbook/gitbook-plugin-expandable-chapters/expandable-chapters.css">
                
            
                
                <link rel="stylesheet" href="../../gitbook/gitbook-plugin-toggle-chapters/toggle.css">
                
            
                
                <link rel="stylesheet" href="../../gitbook/gitbook-plugin-back-to-top-button/plugin.css">
                
            
                
                <link rel="stylesheet" href="../../gitbook/gitbook-plugin-code/plugin.css">
                
            
                
                <link rel="stylesheet" href="../../gitbook/gitbook-plugin-search-pro/search.css">
                
            
                
                <link rel="stylesheet" href="../../gitbook/gitbook-plugin-pageview-count/plugin.css">
                
            
                
                <link rel="stylesheet" href="../../gitbook/gitbook-plugin-highlight/website.css">
                
            
                
                <link rel="stylesheet" href="../../gitbook/gitbook-plugin-fontsettings/website.css">
                
            
        

    

    
        
    

        
    
    
    
    <meta name="HandheldFriendly" content="true"/>
    <meta name="viewport" content="width=device-width, initial-scale=1, user-scalable=no">
    <meta name="apple-mobile-web-app-capable" content="yes">
    <meta name="apple-mobile-web-app-status-bar-style" content="black">
    <link rel="apple-touch-icon-precomposed" sizes="152x152" href="../../gitbook/images/apple-touch-icon-precomposed-152.png">
    <link rel="shortcut icon" href="../../gitbook/images/favicon.ico" type="image/x-icon">

    
    <link rel="next" href="../blockingQueue/1.阻塞队列.html" />
    
    
    <link rel="prev" href="1.AQS.html" />
    

    
    
    
    

    </head>
    <body>
        
<div class="book">
    <div class="book-summary">
        
            
<div id="book-search-input" role="search">
    <input type="text" placeholder="输入并搜索" />
</div>

            
                <nav role="navigation">
                


<ul class="summary">
    
    

    

    
        
        <li class="header">操作系统</li>
        
        
    
        <li class="chapter " data-level="1.1" data-path="../../">
            
                <a href="../../">
            
                    
                    Introduction
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2" data-path="../../操作系统/1.基本概述.html">
            
                <a href="../../操作系统/1.基本概述.html">
            
                    
                    1.操作系统基本概述
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3" data-path="../../操作系统/2.进程管理之进程与线程.html">
            
                <a href="../../操作系统/2.进程管理之进程与线程.html">
            
                    
                    2.进程管理之进程与线程
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.4" data-path="../../操作系统/3.进程管理之处理机调度.html">
            
                <a href="../../操作系统/3.进程管理之处理机调度.html">
            
                    
                    3.进程管理之处理机调度
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.5" data-path="../../操作系统/4.进程管理之进程同步.html">
            
                <a href="../../操作系统/4.进程管理之进程同步.html">
            
                    
                    4.进程管理之进程同步
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.6" data-path="../../操作系统/5.内存管理之基本概念.html">
            
                <a href="../../操作系统/5.内存管理之基本概念.html">
            
                    
                    5.内存管理之基本概念
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.7" data-path="../../操作系统/6.内存管理之虚拟内存.html">
            
                <a href="../../操作系统/6.内存管理之虚拟内存.html">
            
                    
                    6.内存管理之虚拟内存
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.8" data-path="../../操作系统/7.IO设备管理之基本概念.html">
            
                <a href="../../操作系统/7.IO设备管理之基本概念.html">
            
                    
                    7.IO设备管理之基本概念
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.9" data-path="../../操作系统/8.面试题总结一.html">
            
                <a href="../../操作系统/8.面试题总结一.html">
            
                    
                    8.面试题总结一
            
                </a>
            

            
        </li>
    

    
        
        <li class="header">计算机网络篇</li>
        
        
    
        <li class="chapter " data-level="2.1" data-path="../../计算机网络/1.计算机网络入门基础概念.html">
            
                <a href="../../计算机网络/1.计算机网络入门基础概念.html">
            
                    
                    1.计算机网络入门文章(必读)
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="2.2" data-path="../../计算机网络/2.计算机网络通信基础.html">
            
                <a href="../../计算机网络/2.计算机网络通信基础.html">
            
                    
                    2.计算机网络通信入门文章(必读)
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="2.3" data-path="../../计算机网络/3.数据链路层.html">
            
                <a href="../../计算机网络/3.数据链路层.html">
            
                    
                    3.数据链路层
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="2.4" data-path="../../计算机网络/4.网络层1.html">
            
                <a href="../../计算机网络/4.网络层1.html">
            
                    
                    4.网络层(一)
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="2.5" data-path="../../计算机网络/5.网络层2.html">
            
                <a href="../../计算机网络/5.网络层2.html">
            
                    
                    5.网络层(二)
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="2.6" data-path="../../计算机网络/6.传输层.html">
            
                <a href="../../计算机网络/6.传输层.html">
            
                    
                    6.传输层
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="2.7" data-path="../../计算机网络/7.应用层.html">
            
                <a href="../../计算机网络/7.应用层.html">
            
                    
                    7.应用层(⭐)
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="2.8" data-path="../../计算机网络/10.HTTP相关协议.html">
            
                <a href="../../计算机网络/10.HTTP相关协议.html">
            
                    
                    8.HTTP相关及发展
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="2.9" >
            
                <span>
            
                    
                    9.websocket
            
                </span>
            

            
        </li>
    
        <li class="chapter " data-level="2.10" data-path="../../计算机网络/9.面试题二.html">
            
                <a href="../../计算机网络/9.面试题二.html">
            
                    
                    面试题总结一
            
                </a>
            

            
        </li>
    

    
        
        <li class="header">数据结构篇</li>
        
        
    
        <li class="chapter " data-level="3.1" data-path="../../数据结构/1.图论.html">
            
                <a href="../../数据结构/1.图论.html">
            
                    
                    1.图论
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="3.2" data-path="../../数据结构/2.图论例题.html">
            
                <a href="../../数据结构/2.图论例题.html">
            
                    
                    2.图论例题
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="3.3" data-path="../../数据结构/3.散列表.html">
            
                <a href="../../数据结构/3.散列表.html">
            
                    
                    3.散列表
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="3.4" data-path="../../数据结构/4.排序算法.html">
            
                <a href="../../数据结构/4.排序算法.html">
            
                    
                    4.排序算法
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="3.5" data-path="../../数据结构/5.排序列题.html">
            
                <a href="../../数据结构/5.排序列题.html">
            
                    
                    5.排序算法例题
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="3.6" data-path="../../数据结构/6.树论.html">
            
                <a href="../../数据结构/6.树论.html">
            
                    
                    6.树论
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="3.7" data-path="../../数据结构/7.树论例题.html">
            
                <a href="../../数据结构/7.树论例题.html">
            
                    
                    7.树论例题.md
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="3.8" data-path="../../数据结构/9.模板.html">
            
                <a href="../../数据结构/9.模板.html">
            
                    
                    数据结构模板
            
                </a>
            

            
        </li>
    

    
        
        <li class="header">算法篇</li>
        
        
    
        <li class="chapter " data-level="4.1" data-path="../../算法/leetCode周赛题解.html">
            
                <a href="../../算法/leetCode周赛题解.html">
            
                    
                    leetCode周赛记录
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="4.2" data-path="../../算法/1.时间复杂度.html">
            
                <a href="../../算法/1.时间复杂度.html">
            
                    
                    1.时间复杂度
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="4.3" data-path="../../算法/2.深度优先搜索.html">
            
                <a href="../../算法/2.深度优先搜索.html">
            
                    
                    2.深度优先搜索算法
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="4.4" data-path="../../算法/2.深度优先搜索题目集.html">
            
                <a href="../../算法/2.深度优先搜索题目集.html">
            
                    
                    2.深搜题目集
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="4.5" data-path="../../算法/3.广度优先搜索.html">
            
                <a href="../../算法/3.广度优先搜索.html">
            
                    
                    3.广度优先搜索算法
            
                </a>
            

            
        </li>
    

    
        
        <li class="header">JVM篇</li>
        
        
    
        <li class="chapter " data-level="5.1" data-path="../../jvm/1.jvm内存结构.html">
            
                <a href="../../jvm/1.jvm内存结构.html">
            
                    
                    1.jvm内存结构
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="5.2" data-path="../../jvm/2.HotSpot虚拟机对象探秘.html">
            
                <a href="../../jvm/2.HotSpot虚拟机对象探秘.html">
            
                    
                    2.HotSpot虚拟机对象探秘.md
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="5.3" data-path="../../jvm/3.对象存亡与垃圾收集算法.html">
            
                <a href="../../jvm/3.对象存亡与垃圾收集算法.html">
            
                    
                    3.对象存亡与垃圾收集算法.md
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="5.4" data-path="../../jvm/4.HotSpot的算法细节与垃圾收集器.html">
            
                <a href="../../jvm/4.HotSpot的算法细节与垃圾收集器.html">
            
                    
                    4.HotSpot的算法细节与垃圾收集器.md
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="5.5" data-path="../../jvm/5.内存分配与回收策略.html">
            
                <a href="../../jvm/5.内存分配与回收策略.html">
            
                    
                    5.内存分配与回收策略.md
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="5.6" data-path="../../jvm/6.类文件结构.html">
            
                <a href="../../jvm/6.类文件结构.html">
            
                    
                    6.类文件结构.md
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="5.7" data-path="../../jvm/7.类加载机制.html">
            
                <a href="../../jvm/7.类加载机制.html">
            
                    
                    7.类加载机制.md
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="5.8" data-path="../../jvm/8.jvm逃逸分析.html">
            
                <a href="../../jvm/8.jvm逃逸分析.html">
            
                    
                    8.逃逸分析技术
            
                </a>
            

            
        </li>
    

    
        
        <li class="header">多线程篇</li>
        
        
    
        <li class="chapter " data-level="6.1" data-path="../basic/0.多线程路线图.html">
            
                <a href="../basic/0.多线程路线图.html">
            
                    
                    1.多线程学习路线图
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="6.2" data-path="../basic/0.java内存模型与线程.html">
            
                <a href="../basic/0.java内存模型与线程.html">
            
                    
                    2.java内存模型与线程
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="6.3" data-path="../3.并行程序基础.html">
            
                <a href="../3.并行程序基础.html">
            
                    
                    2.并行程序基础
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="6.4" data-path="../basic/0.jhappenBefore与jvm指令重排.html">
            
                <a href="../basic/0.jhappenBefore与jvm指令重排.html">
            
                    
                    3.happenBefore与jvm指令重排序
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="6.5" data-path="../basic/1.synchronized与volatile.html">
            
                <a href="../basic/1.synchronized与volatile.html">
            
                    
                    4.synchronized与volatile.md
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="6.6" data-path="../basic/2.ThreadLocal.html">
            
                <a href="../basic/2.ThreadLocal.html">
            
                    
                    5.ThreadLocal.md
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="6.7" data-path="../cas/1.乐观锁与悲观锁.html">
            
                <a href="../cas/1.乐观锁与悲观锁.html">
            
                    
                    6.乐观锁与悲观锁.md
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="6.8" data-path="../cas/2.CAS与atomic类.html">
            
                <a href="../cas/2.CAS与atomic类.html">
            
                    
                    7.CAS与atomic类.md
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="6.9" data-path="1.AQS.html">
            
                <a href="1.AQS.html">
            
                    
                    8.AQS.md
            
                </a>
            

            
        </li>
    
        <li class="chapter active" data-level="6.10" data-path="2.基于AQS的并发工具.html">
            
                <a href="2.基于AQS的并发工具.html">
            
                    
                    9.基于AQS的并发工具.md
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="6.11" data-path="../blockingQueue/1.阻塞队列.html">
            
                <a href="../blockingQueue/1.阻塞队列.html">
            
                    
                    10.阻塞队列.md
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="6.12" data-path="../pond/1.线程池与底层原理.html">
            
                <a href="../pond/1.线程池与底层原理.html">
            
                    
                    11.线程池与底层原理.md
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="6.13" data-path="../10面试题一.html">
            
                <a href="../10面试题一.html">
            
                    
                    12.面试题一
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="6.14" data-path="../10面试题二.html">
            
                <a href="../10面试题二.html">
            
                    
                    13.面试题二.md
            
                </a>
            

            
        </li>
    

    
        
        <li class="header">java集合篇</li>
        
        
    
        <li class="chapter " data-level="7.1" data-path="../../basic/collection/1.java集合.html">
            
                <a href="../../basic/collection/1.java集合.html">
            
                    
                    1.集合架构
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="7.2" data-path="../../basic/collection/2.HashMap源码分析.html">
            
                <a href="../../basic/collection/2.HashMap源码分析.html">
            
                    
                    2.hashMap底层原理
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="7.3" data-path="../../basic/collection/3.ConcurrentHashMap分析.html">
            
                <a href="../../basic/collection/3.ConcurrentHashMap分析.html">
            
                    
                    3.ConcurrentHashMap分析
            
                </a>
            

            
        </li>
    

    
        
        <li class="header">设计模式篇</li>
        
        
    
        <li class="chapter " data-level="8.1" >
            
                <span>
            
                    
                    1.创造型模式
            
                </span>
            

            
        </li>
    
        <li class="chapter " data-level="8.2" data-path="../../设计模式/factory/三种工厂模式介绍.html">
            
                <a href="../../设计模式/factory/三种工厂模式介绍.html">
            
                    
                    三种工厂模式
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="8.3" data-path="../../设计模式/factory/实战工厂模式.html">
            
                <a href="../../设计模式/factory/实战工厂模式.html">
            
                    
                    实战工厂模式
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="8.4" data-path="../../设计模式/factory/实战抽象工厂模式.html">
            
                <a href="../../设计模式/factory/实战抽象工厂模式.html">
            
                    
                    实战抽象工厂模式
            
                </a>
            

            
        </li>
    

    
        
        <li class="header">mysql篇</li>
        
        
    
        <li class="chapter " data-level="9.1" data-path="../../数据库/mysql/1.b树b+树的底层原理.html">
            
                <a href="../../数据库/mysql/1.b树b+树的底层原理.html">
            
                    
                    1.mysql索引基础之b树与b+树⭐⭐
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="9.2" data-path="../../数据库/mysql/2.mysql索引.html">
            
                <a href="../../数据库/mysql/2.mysql索引.html">
            
                    
                    2.mysql索引及优化⭐⭐⭐⭐
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="9.3" data-path="../../数据库/mysql/3.mysql存储引擎.html">
            
                <a href="../../数据库/mysql/3.mysql存储引擎.html">
            
                    
                    3.mysql存储引擎.md
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="9.4" data-path="../../数据库/mysql/4.多版本并发控制之mvcc原理.html">
            
                <a href="../../数据库/mysql/4.多版本并发控制之mvcc原理.html">
            
                    
                    4.多版本并发控制之mvcc原理
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="9.5" data-path="../../数据库/mysql/4.mysql事务.html">
            
                <a href="../../数据库/mysql/4.mysql事务.html">
            
                    
                    5.mysql事务
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="9.6" data-path="../../数据库/mysql/5.mysql锁机制.html">
            
                <a href="../../数据库/mysql/5.mysql锁机制.html">
            
                    
                    6.mysql的锁机制
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="9.7" data-path="../../数据库/mysql/6.mysql面试题一.html">
            
                <a href="../../数据库/mysql/6.mysql面试题一.html">
            
                    
                    7.mysql面试题一
            
                </a>
            

            
        </li>
    

    
        
        <li class="header">高并发中间件篇</li>
        
        
    

    
        
        <li class="header">架构理论篇</li>
        
        
    
        <li class="chapter " data-level="11.1" data-path="../../架构/0.架构演进.html">
            
                <a href="../../架构/0.架构演进.html">
            
                    
                    1.架构演进
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="11.2" data-path="../../架构/0.微服务架构与soa架构.html">
            
                <a href="../../架构/0.微服务架构与soa架构.html">
            
                    
                    2.微服务架构与soa架构
            
                </a>
            

            
        </li>
    

    
        
        <li class="header">高可用系统篇</li>
        
        
    
        <li class="chapter " data-level="12.1" data-path="../../高可用架构/1.限流.html">
            
                <a href="../../高可用架构/1.限流.html">
            
                    
                    1.系统限流
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="12.2" data-path="../../高可用系统/2.服务熔断.html">
            
                <a href="../../高可用系统/2.服务熔断.html">
            
                    
                    2.服务熔断
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="12.3" data-path="../../高可用系统/3.服务降级.html">
            
                <a href="../../高可用系统/3.服务降级.html">
            
                    
                    3.服务降级
            
                </a>
            

            
        </li>
    

    
        
        <li class="header">spring-cloud 微服务篇</li>
        
        
    

    

    <li class="divider"></li>

    <li>
        <a href="https://www.gitbook.com" target="blank" class="gitbook-link">
            本书使用 GitBook 发布
        </a>
    </li>
</ul>


                </nav>
            
        
    </div>

    <div class="book-body">
        
            <div class="body-inner">
                
                    

<div class="book-header" role="navigation">
    

    <!-- Title -->
    <h1>
        <i class="fa fa-circle-o-notch fa-spin"></i>
        <a href="../.." >9.基于AQS的并发工具.md</a>
    </h1>
</div>




                    <div class="page-wrapper" tabindex="-1" role="main">
                        <div class="page-inner">
                            
<div id="book-search-results">
    <div class="search-noresults">
    
                                <section class="normal markdown-section">
                                
                                <div id="anchor-navigation-ex-navbar"><i class="fa fa-navicon"></i><ul><ul><li><span class="title-icon "></span><a href="#&#x4E00;-reentrantlock"><b></b>&#x4E00; ReentrantLock</a></li><ul><li><span class="title-icon "></span><a href="#&#x83B7;&#x53D6;&#x9501;"><b></b>&#x83B7;&#x53D6;&#x9501;</a></li><li><span class="title-icon "></span><a href="#&#x91CA;&#x653E;&#x9501;"><b></b>&#x91CA;&#x653E;&#x9501;</a></li><li><span class="title-icon "></span><a href="#&#x516C;&#x5E73;&#x9501;&#x4E0E;&#x975E;&#x516C;&#x5E73;&#x9501;"><b></b>&#x516C;&#x5E73;&#x9501;&#x4E0E;&#x975E;&#x516C;&#x5E73;&#x9501;</a></li><li><span class="title-icon "></span><a href="#reentrantlock&#x4E0E;synchronized&#x7684;&#x533A;&#x522B;"><b></b>ReentrantLock&#x4E0E;synchronized&#x7684;&#x533A;&#x522B;</a></li></ul><li><span class="title-icon "></span><a href="#&#x4E8C;-condition"><b></b>&#x4E8C; condition</a></li><ul><li><span class="title-icon "></span><a href="#&#x7B49;&#x5F85;&#x961F;&#x5217;"><b></b>&#x7B49;&#x5F85;&#x961F;&#x5217;</a></li><li><span class="title-icon "></span><a href="#&#x7B49;&#x5F85;"><b></b>&#x7B49;&#x5F85;</a></li><li><span class="title-icon "></span><a href="#&#x901A;&#x77E5;"><b></b>&#x901A;&#x77E5;</a></li><li><span class="title-icon "></span><a href="#&#x603B;&#x7ED3;"><b></b>&#x603B;&#x7ED3;</a></li><li><span class="title-icon "></span><a href="#condition&#x7684;&#x5E94;&#x7528;"><b></b>Condition&#x7684;&#x5E94;&#x7528;</a></li></ul><li><span class="title-icon "></span><a href="#&#x4E09;-reentrantreadwritelock"><b></b>&#x4E09; ReentrantReadWriteLock</a></li><ul><li><span class="title-icon "></span><a href="#&#x5199;&#x9501;"><b></b>&#x5199;&#x9501;</a></li><li><span class="title-icon "></span><a href="#&#x8BFB;&#x9501;"><b></b>&#x8BFB;&#x9501;</a></li><li><span class="title-icon "></span><a href="#&#x9501;&#x964D;&#x7EA7;"><b></b>&#x9501;&#x964D;&#x7EA7;</a></li></ul><li><span class="title-icon "></span><a href="#&#x56DB;-cyclicbarrier"><b></b>&#x56DB; CyclicBarrier</a></li><ul><li><span class="title-icon "></span><a href="#&#x5B9E;&#x73B0;&#x5206;&#x6790;"><b></b>&#x5B9E;&#x73B0;&#x5206;&#x6790;</a></li><li><span class="title-icon "></span><a href="#&#x5E94;&#x7528;&#x573A;&#x666F;"><b></b>&#x5E94;&#x7528;&#x573A;&#x666F;</a></li><li><span class="title-icon "></span><a href="#&#x5E94;&#x7528;&#x793A;&#x4F8B;"><b></b>&#x5E94;&#x7528;&#x793A;&#x4F8B;</a></li></ul><li><span class="title-icon "></span><a href="#&#x4E94;-countdownlatch"><b></b>&#x4E94; countDownLatch</a></li><ul><li><span class="title-icon "></span><a href="#&#x5B9E;&#x73B0;&#x5206;&#x6790;_1"><b></b>&#x5B9E;&#x73B0;&#x5206;&#x6790;</a></li><li><span class="title-icon "></span><a href="#await"><b></b>await()</a></li><li><span class="title-icon "></span><a href="#countdown"><b></b>countDown()</a></li><li><span class="title-icon "></span><a href="#&#x603B;&#x7ED3;_1"><b></b>&#x603B;&#x7ED3;</a></li><li><span class="title-icon "></span><a href="#&#x5E94;&#x7528;&#x793A;&#x4F8B;_1"><b></b>&#x5E94;&#x7528;&#x793A;&#x4F8B;</a></li></ul></ul></ul></div><a href="#" id="anchorNavigationExGoTop"><i class="fa fa-arrow-up"></i></a><h2 id="&#x4E00;-reentrantlock"><a name="&#x4E00;-reentrantlock" class="anchor-navigation-ex-anchor" href="#&#x4E00;-reentrantlock"><i class="fa fa-link" aria-hidden="true"></i></a><a name="&#x4E00;-reentrantlock" class="plugin-anchor" href="#&#x4E00;-reentrantlock"><i class="fa fa-link" aria-hidden="true"></i></a>&#x4E00; ReentrantLock</h2>
<p>ReentrantLock&#xFF0C;&#x53EF;&#x91CD;&#x5165;&#x9501;&#xFF0C;&#x662F;&#x4E00;&#x79CD;&#x9012;&#x5F52;&#x65E0;&#x963B;&#x585E;&#x7684;&#x540C;&#x6B65;&#x673A;&#x5236;&#x3002;&#x5B83;&#x53EF;&#x4EE5;&#x7B49;&#x540C;&#x4E8E;synchronized&#x7684;&#x4F7F;&#x7528;&#xFF0C;&#x4F46;&#x662F;ReentrantLock&#x63D0;&#x4F9B;&#x4E86;&#x6BD4;synchronized&#x66F4;&#x5F3A;&#x5927;&#x3001;&#x7075;&#x6D3B;&#x7684;&#x9501;&#x673A;&#x5236;&#xFF0C;&#x53EF;&#x4EE5;&#x51CF;&#x5C11;&#x6B7B;&#x9501;&#x53D1;&#x751F;&#x7684;&#x6982;&#x7387;&#x3002; API&#x4ECB;&#x7ECD;&#x5982;&#x4E0B;&#xFF1A;</p>
<blockquote>
<p>&#x4E00;&#x4E2A;&#x53EF;&#x91CD;&#x5165;&#x7684;&#x4E92;&#x65A5;&#x9501;&#x5B9A; Lock&#xFF0C;&#x5B83;&#x5177;&#x6709;&#x4E0E;&#x4F7F;&#x7528; synchronized &#x65B9;&#x6CD5;&#x548C;&#x8BED;&#x53E5;&#x6240;&#x8BBF;&#x95EE;&#x7684;&#x9690;&#x5F0F;&#x76D1;&#x89C6;&#x5668;&#x9501;&#x5B9A;&#x76F8;&#x540C;&#x7684;&#x4E00;&#x4E9B;&#x57FA;&#x672C;&#x884C;&#x4E3A;&#x548C;&#x8BED;&#x4E49;&#xFF0C;&#x4F46;&#x529F;&#x80FD;&#x66F4;&#x5F3A;&#x5927;&#x3002;ReentrantLock &#x5C06;&#x7531;&#x6700;&#x8FD1;&#x6210;&#x529F;&#x83B7;&#x5F97;&#x9501;&#x5B9A;&#xFF0C;&#x5E76;&#x4E14;&#x8FD8;&#x6CA1;&#x6709;&#x91CA;&#x653E;&#x8BE5;&#x9501;&#x5B9A;&#x7684;&#x7EBF;&#x7A0B;&#x6240;&#x62E5;&#x6709;&#x3002;&#x5F53;&#x9501;&#x5B9A;&#x6CA1;&#x6709;&#x88AB;&#x53E6;&#x4E00;&#x4E2A;&#x7EBF;&#x7A0B;&#x6240;&#x62E5;&#x6709;&#x65F6;&#xFF0C;&#x8C03;&#x7528; lock &#x7684;&#x7EBF;&#x7A0B;&#x5C06;&#x6210;&#x529F;&#x83B7;&#x53D6;&#x8BE5;&#x9501;&#x5B9A;&#x5E76;&#x8FD4;&#x56DE;&#x3002;&#x5982;&#x679C;&#x5F53;&#x524D;&#x7EBF;&#x7A0B;&#x5DF2;&#x7ECF;&#x62E5;&#x6709;&#x8BE5;&#x9501;&#x5B9A;&#xFF0C;&#x6B64;&#x65B9;&#x6CD5;&#x5C06;&#x7ACB;&#x5373;&#x8FD4;&#x56DE;&#x3002;&#x53EF;&#x4EE5;&#x4F7F;&#x7528; isHeldByCurrentThread() &#x548C; getHoldCount() &#x65B9;&#x6CD5;&#x6765;&#x68C0;&#x67E5;&#x6B64;&#x60C5;&#x51B5;&#x662F;&#x5426;&#x53D1;&#x751F;&#x3002;</p>
</blockquote>
<p>ReentrantLock&#x8FD8;&#x63D0;&#x4F9B;&#x4E86;&#x516C;&#x5E73;&#x9501;&#x4E5F;&#x975E;&#x516C;&#x5E73;&#x9501;&#x7684;&#x9009;&#x62E9;&#xFF0C;&#x6784;&#x9020;&#x65B9;&#x6CD5;&#x63A5;&#x53D7;&#x4E00;&#x4E2A;&#x53EF;&#x9009;&#x7684;&#x516C;&#x5E73;&#x53C2;&#x6570;&#xFF08;&#x9ED8;&#x8BA4;&#x975E;&#x516C;&#x5E73;&#x9501;&#xFF09;&#xFF0C;&#x5F53;&#x8BBE;&#x7F6E;&#x4E3A;true&#x65F6;&#xFF0C;&#x8868;&#x793A;&#x516C;&#x5E73;&#x9501;&#xFF0C;&#x5426;&#x5219;&#x4E3A;&#x975E;&#x516C;&#x5E73;&#x9501;&#x3002;&#x516C;&#x5E73;&#x9501;&#x4E0E;&#x975E;&#x516C;&#x5E73;&#x9501;&#x7684;&#x533A;&#x522B;&#x5728;&#x4E8E;&#x516C;&#x5E73;&#x9501;&#x7684;&#x9501;&#x83B7;&#x53D6;&#x662F;&#x6709;&#x987A;&#x5E8F;&#x7684;&#x3002;&#x4F46;&#x662F;&#x516C;&#x5E73;&#x9501;&#x7684;&#x6548;&#x7387;&#x5F80;&#x5F80;&#x6CA1;&#x6709;&#x975E;&#x516C;&#x5E73;&#x9501;&#x7684;&#x6548;&#x7387;&#x9AD8;&#xFF0C;&#x5728;&#x8BB8;&#x591A;&#x7EBF;&#x7A0B;&#x8BBF;&#x95EE;&#x7684;&#x60C5;&#x51B5;&#x4E0B;&#xFF0C;&#x516C;&#x5E73;&#x9501;&#x8868;&#x73B0;&#x51FA;&#x8F83;&#x4F4E;&#x7684;&#x541E;&#x5410;&#x91CF;&#x3002;</p>
<p><img src="https://gitee.com/zisuu/picture/raw/master/img/20201216194456.png" alt="image-20201216194456165"></p>
<h3 id="&#x83B7;&#x53D6;&#x9501;"><a name="&#x83B7;&#x53D6;&#x9501;" class="anchor-navigation-ex-anchor" href="#&#x83B7;&#x53D6;&#x9501;"><i class="fa fa-link" aria-hidden="true"></i></a><a name="&#x83B7;&#x53D6;&#x9501;" class="plugin-anchor" href="#&#x83B7;&#x53D6;&#x9501;"><i class="fa fa-link" aria-hidden="true"></i></a>&#x83B7;&#x53D6;&#x9501;</h3>
<p>&#x6211;&#x4EEC;&#x4E00;&#x822C;&#x90FD;&#x662F;&#x8FD9;&#x4E48;&#x4F7F;&#x7528;ReentrantLock&#x83B7;&#x53D6;&#x9501;&#x7684;&#xFF1A;</p>
<pre><code class="lang-java"><span class="hljs-comment">//&#x975E;&#x516C;&#x5E73;&#x9501;</span>
ReentrantLock lock = <span class="hljs-keyword">new</span> ReentrantLock();
lock.lock();
</code></pre>
<p>lock&#x65B9;&#x6CD5;&#xFF1A;</p>
<pre><code class="lang-java">    <span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-keyword">void</span> <span class="hljs-title">lock</span><span class="hljs-params">()</span> </span>{
        sync.lock();
    }
</code></pre>
<p>Sync&#x4E3A;ReentrantLock&#x91CC;&#x9762;&#x7684;&#x4E00;&#x4E2A;&#x5185;&#x90E8;&#x7C7B;&#xFF0C;&#x5B83;&#x7EE7;&#x627F;AQS&#xFF08;AbstractQueuedSynchronizer&#xFF09;&#xFF0C;&#x5B83;&#x6709;&#x4E24;&#x4E2A;&#x5B50;&#x7C7B;&#xFF1A;&#x516C;&#x5E73;&#x9501;FairSync&#x548C;&#x975E;&#x516C;&#x5E73;&#x9501;NonfairSync&#x3002; ReentrantLock&#x91CC;&#x9762;&#x5927;&#x90E8;&#x5206;&#x7684;&#x529F;&#x80FD;&#x90FD;&#x662F;&#x59D4;&#x6258;&#x7ED9;Sync&#x6765;&#x5B9E;&#x73B0;&#x7684;&#xFF0C;&#x540C;&#x65F6;Sync&#x5185;&#x90E8;&#x5B9A;&#x4E49;&#x4E86;lock()&#x62BD;&#x8C61;&#x65B9;&#x6CD5;&#x7531;&#x5176;&#x5B50;&#x7C7B;&#x53BB;&#x5B9E;&#x73B0;&#xFF0C;&#x9ED8;&#x8BA4;&#x5B9E;&#x73B0;&#x4E86;nonfairTryAcquire(int acquires)&#x65B9;&#x6CD5;&#xFF0C;&#x53EF;&#x4EE5;&#x770B;&#x51FA;&#x5B83;&#x662F;&#x975E;&#x516C;&#x5E73;&#x9501;&#x7684;&#x9ED8;&#x8BA4;&#x5B9E;&#x73B0;&#x65B9;&#x5F0F;&#x3002;&#x4E0B;&#x9762;&#x6211;&#x4EEC;&#x770B;&#x975E;&#x516C;&#x5E73;&#x9501;&#x7684;lock()&#x65B9;&#x6CD5;&#xFF1A;</p>
<pre><code class="lang-java">    <span class="hljs-function"><span class="hljs-keyword">final</span> <span class="hljs-keyword">void</span> <span class="hljs-title">lock</span><span class="hljs-params">()</span> </span>{
        <span class="hljs-comment">//&#x5C1D;&#x8BD5;&#x83B7;&#x53D6;&#x9501;</span>
        <span class="hljs-keyword">if</span> (compareAndSetState(<span class="hljs-number">0</span>, <span class="hljs-number">1</span>))
            setExclusiveOwnerThread(Thread.currentThread());
        <span class="hljs-keyword">else</span>
            <span class="hljs-comment">//&#x83B7;&#x53D6;&#x5931;&#x8D25;&#xFF0C;&#x8C03;&#x7528;AQS&#x7684;acquire(int arg)&#x65B9;&#x6CD5;</span>
            acquire(<span class="hljs-number">1</span>);
    }
</code></pre>
<p>&#x9996;&#x5148;&#x4F1A;&#x7B2C;&#x4E00;&#x6B21;&#x5C1D;&#x8BD5;&#x5FEB;&#x901F;&#x83B7;&#x53D6;&#x9501;&#xFF0C;&#x5982;&#x679C;&#x83B7;&#x53D6;&#x5931;&#x8D25;&#xFF0C;&#x5219;&#x8C03;&#x7528;acquire(int arg)&#x65B9;&#x6CD5;&#xFF0C;&#x8BE5;&#x65B9;&#x6CD5;&#x5B9A;&#x4E49;&#x5728;AQS&#x4E2D;&#xFF0C;&#x5982;&#x4E0B;&#xFF1A;</p>
<pre><code class="lang-java">    <span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-keyword">final</span> <span class="hljs-keyword">void</span> <span class="hljs-title">acquire</span><span class="hljs-params">(<span class="hljs-keyword">int</span> arg)</span> </span>{
        <span class="hljs-keyword">if</span> (!tryAcquire(arg) &amp;&amp;
                acquireQueued(addWaiter(Node.EXCLUSIVE), arg))
            selfInterrupt();
    }
</code></pre>
<p>&#x8FD9;&#x4E2A;&#x65B9;&#x6CD5;&#x9996;&#x5148;&#x8C03;&#x7528;tryAcquire(int arg)&#x65B9;&#x6CD5;&#xFF0C;&#x5728;AQS&#x4E2D;&#x8BB2;&#x8FF0;&#x8FC7;&#xFF0C;tryAcquire(int arg)&#x9700;&#x8981;&#x81EA;&#x5B9A;&#x4E49;&#x540C;&#x6B65;&#x7EC4;&#x4EF6;&#x63D0;&#x4F9B;&#x5B9E;&#x73B0;&#xFF0C;&#x975E;&#x516C;&#x5E73;&#x9501;&#x5B9E;&#x73B0;&#x5982;&#x4E0B;&#xFF1A;</p>
<pre><code class="lang-java">    <span class="hljs-function"><span class="hljs-keyword">protected</span> <span class="hljs-keyword">final</span> <span class="hljs-keyword">boolean</span> <span class="hljs-title">tryAcquire</span><span class="hljs-params">(<span class="hljs-keyword">int</span> acquires)</span> </span>{
        <span class="hljs-keyword">return</span> nonfairTryAcquire(acquires);
    }

    <span class="hljs-function"><span class="hljs-keyword">final</span> <span class="hljs-keyword">boolean</span> <span class="hljs-title">nonfairTryAcquire</span><span class="hljs-params">(<span class="hljs-keyword">int</span> acquires)</span> </span>{
        <span class="hljs-comment">//&#x5F53;&#x524D;&#x7EBF;&#x7A0B;</span>
        <span class="hljs-keyword">final</span> Thread current = Thread.currentThread();
        <span class="hljs-comment">//&#x83B7;&#x53D6;&#x540C;&#x6B65;&#x72B6;&#x6001;</span>
        <span class="hljs-keyword">int</span> c = getState();
        <span class="hljs-comment">//state == 0,&#x8868;&#x793A;&#x6CA1;&#x6709;&#x8BE5;&#x9501;&#x5904;&#x4E8E;&#x7A7A;&#x95F2;&#x72B6;&#x6001;</span>
        <span class="hljs-keyword">if</span> (c == <span class="hljs-number">0</span>) {
            <span class="hljs-comment">//&#x83B7;&#x53D6;&#x9501;&#x6210;&#x529F;&#xFF0C;&#x8BBE;&#x7F6E;&#x4E3A;&#x5F53;&#x524D;&#x7EBF;&#x7A0B;&#x6240;&#x6709;</span>
            <span class="hljs-keyword">if</span> (compareAndSetState(<span class="hljs-number">0</span>, acquires)) {
                setExclusiveOwnerThread(current);
                <span class="hljs-keyword">return</span> <span class="hljs-keyword">true</span>;
            }
        }
        <span class="hljs-comment">//&#x7EBF;&#x7A0B;&#x91CD;&#x5165;</span>
        <span class="hljs-comment">//&#x5224;&#x65AD;&#x9501;&#x6301;&#x6709;&#x7684;&#x7EBF;&#x7A0B;&#x662F;&#x5426;&#x4E3A;&#x5F53;&#x524D;&#x7EBF;&#x7A0B;</span>
        <span class="hljs-keyword">else</span> <span class="hljs-keyword">if</span> (current == getExclusiveOwnerThread()) {
            <span class="hljs-keyword">int</span> nextc = c + acquires;
            <span class="hljs-keyword">if</span> (nextc &lt; <span class="hljs-number">0</span>) <span class="hljs-comment">// overflow</span>
                <span class="hljs-keyword">throw</span> <span class="hljs-keyword">new</span> Error(<span class="hljs-string">&quot;Maximum lock count exceeded&quot;</span>);
            setState(nextc);
            <span class="hljs-keyword">return</span> <span class="hljs-keyword">true</span>;
        }
        <span class="hljs-keyword">return</span> <span class="hljs-keyword">false</span>;
    }
</code></pre>
<p>&#x8BE5;&#x65B9;&#x6CD5;&#x4E3B;&#x8981;&#x903B;&#x8F91;&#xFF1A;&#x9996;&#x5148;&#x5224;&#x65AD;&#x540C;&#x6B65;&#x72B6;&#x6001;state == 0 ?&#xFF0C;&#x5982;&#x679C;&#x662F;&#x8868;&#x793A;&#x8BE5;&#x9501;&#x8FD8;&#x6CA1;&#x6709;&#x88AB;&#x7EBF;&#x7A0B;&#x6301;&#x6709;&#xFF0C;&#x76F4;&#x63A5;&#x901A;&#x8FC7;CAS&#x83B7;&#x53D6;&#x540C;&#x6B65;&#x72B6;&#x6001;&#xFF0C;&#x5982;&#x679C;&#x6210;&#x529F;&#x8FD4;&#x56DE;true&#x3002;&#x5982;&#x679C;state != 0&#xFF0C;&#x5219;&#x5224;&#x65AD;&#x5F53;&#x524D;&#x7EBF;&#x7A0B;&#x662F;&#x5426;&#x4E3A;&#x83B7;&#x53D6;&#x9501;&#x7684;&#x7EBF;&#x7A0B;&#xFF0C;&#x5982;&#x679C;&#x662F;&#x5219;&#x83B7;&#x53D6;&#x9501;&#xFF0C;&#x6210;&#x529F;&#x8FD4;&#x56DE;true&#x3002;&#x6210;&#x529F;&#x83B7;&#x53D6;&#x9501;&#x7684;&#x7EBF;&#x7A0B;&#x518D;&#x6B21;&#x83B7;&#x53D6;&#x9501;&#xFF0C;&#x8FD9;&#x662F;&#x589E;&#x52A0;&#x4E86;&#x540C;&#x6B65;&#x72B6;&#x6001;state,&#x8FD9;&#x91CC;&#x5C31;&#x662F;&#x53EF;&#x91CD;&#x5165;&#x7684;&#x6982;&#x5FF5;&#x3002;</p>
<h3 id="&#x91CA;&#x653E;&#x9501;"><a name="&#x91CA;&#x653E;&#x9501;" class="anchor-navigation-ex-anchor" href="#&#x91CA;&#x653E;&#x9501;"><i class="fa fa-link" aria-hidden="true"></i></a><a name="&#x91CA;&#x653E;&#x9501;" class="plugin-anchor" href="#&#x91CA;&#x653E;&#x9501;"><i class="fa fa-link" aria-hidden="true"></i></a>&#x91CA;&#x653E;&#x9501;</h3>
<p>&#x83B7;&#x53D6;&#x540C;&#x6B65;&#x9501;&#x540E;&#xFF0C;&#x4F7F;&#x7528;&#x5B8C;&#x6BD5;&#x5219;&#x9700;&#x8981;&#x91CA;&#x653E;&#x9501;&#xFF0C;ReentrantLock&#x63D0;&#x4F9B;&#x4E86;unlock&#x91CA;&#x653E;&#x9501;&#xFF1A;</p>
<pre><code class="lang-java">    <span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-keyword">void</span> <span class="hljs-title">unlock</span><span class="hljs-params">()</span> </span>{
        sync.release(<span class="hljs-number">1</span>);
    }
</code></pre>
<p>unlock&#x5185;&#x90E8;&#x4F7F;&#x7528;Sync&#x7684;release(int arg)&#x91CA;&#x653E;&#x9501;&#xFF0C;release(int arg)&#x662F;&#x5728;AQS&#x4E2D;&#x5B9A;&#x4E49;&#x7684;&#xFF1A;</p>
<pre><code class="lang-java">    <span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-keyword">final</span> <span class="hljs-keyword">boolean</span> <span class="hljs-title">release</span><span class="hljs-params">(<span class="hljs-keyword">int</span> arg)</span> </span>{
        <span class="hljs-keyword">if</span> (tryRelease(arg)) {
            Node h = head;
            <span class="hljs-keyword">if</span> (h != <span class="hljs-keyword">null</span> &amp;&amp; h.waitStatus != <span class="hljs-number">0</span>)
                unparkSuccessor(h);
            <span class="hljs-keyword">return</span> <span class="hljs-keyword">true</span>;
        }
        <span class="hljs-keyword">return</span> <span class="hljs-keyword">false</span>;
    }
</code></pre>
<p>&#x4E0E;&#x83B7;&#x53D6;&#x540C;&#x6B65;&#x72B6;&#x6001;&#x7684;acquire(int arg)&#x65B9;&#x6CD5;&#x76F8;&#x4F3C;&#xFF0C;&#x91CA;&#x653E;&#x540C;&#x6B65;&#x72B6;&#x6001;&#x7684;tryRelease(int arg)&#x540C;&#x6837;&#x662F;&#x9700;&#x8981;&#x81EA;&#x5B9A;&#x4E49;&#x540C;&#x6B65;&#x7EC4;&#x4EF6;&#x81EA;&#x5DF1;&#x5B9E;&#x73B0;&#xFF1A;</p>
<pre><code class="lang-java">    <span class="hljs-function"><span class="hljs-keyword">protected</span> <span class="hljs-keyword">final</span> <span class="hljs-keyword">boolean</span> <span class="hljs-title">tryRelease</span><span class="hljs-params">(<span class="hljs-keyword">int</span> releases)</span> </span>{
        <span class="hljs-comment">//&#x51CF;&#x6389;releases</span>
        <span class="hljs-keyword">int</span> c = getState() - releases;
        <span class="hljs-comment">//&#x5982;&#x679C;&#x91CA;&#x653E;&#x7684;&#x4E0D;&#x662F;&#x6301;&#x6709;&#x9501;&#x7684;&#x7EBF;&#x7A0B;&#xFF0C;&#x629B;&#x51FA;&#x5F02;&#x5E38;</span>
        <span class="hljs-keyword">if</span> (Thread.currentThread() != getExclusiveOwnerThread())
            <span class="hljs-keyword">throw</span> <span class="hljs-keyword">new</span> IllegalMonitorStateException();
        <span class="hljs-keyword">boolean</span> free = <span class="hljs-keyword">false</span>;
        <span class="hljs-comment">//state == 0 &#x8868;&#x793A;&#x5DF2;&#x7ECF;&#x91CA;&#x653E;&#x5B8C;&#x5168;&#x4E86;&#xFF0C;&#x5176;&#x4ED6;&#x7EBF;&#x7A0B;&#x53EF;&#x4EE5;&#x83B7;&#x53D6;&#x540C;&#x6B65;&#x72B6;&#x6001;&#x4E86;</span>
        <span class="hljs-keyword">if</span> (c == <span class="hljs-number">0</span>) {
            free = <span class="hljs-keyword">true</span>;
            setExclusiveOwnerThread(<span class="hljs-keyword">null</span>);
        }
        setState(c);
        <span class="hljs-keyword">return</span> free;
    }
</code></pre>
<p>&#x53EA;&#x6709;&#x5F53;&#x540C;&#x6B65;&#x72B6;&#x6001;&#x5F7B;&#x5E95;&#x91CA;&#x653E;&#x540E;&#x8BE5;&#x65B9;&#x6CD5;&#x624D;&#x4F1A;&#x8FD4;&#x56DE;true&#x3002;&#x5F53;state == 0 &#x65F6;&#xFF0C;&#x5219;&#x5C06;&#x9501;&#x6301;&#x6709;&#x7EBF;&#x7A0B;&#x8BBE;&#x7F6E;&#x4E3A;null&#xFF0C;free= true&#xFF0C;&#x8868;&#x793A;&#x91CA;&#x653E;&#x6210;&#x529F;&#x3002;</p>
<h3 id="&#x516C;&#x5E73;&#x9501;&#x4E0E;&#x975E;&#x516C;&#x5E73;&#x9501;"><a name="&#x516C;&#x5E73;&#x9501;&#x4E0E;&#x975E;&#x516C;&#x5E73;&#x9501;" class="anchor-navigation-ex-anchor" href="#&#x516C;&#x5E73;&#x9501;&#x4E0E;&#x975E;&#x516C;&#x5E73;&#x9501;"><i class="fa fa-link" aria-hidden="true"></i></a><a name="&#x516C;&#x5E73;&#x9501;&#x4E0E;&#x975E;&#x516C;&#x5E73;&#x9501;" class="plugin-anchor" href="#&#x516C;&#x5E73;&#x9501;&#x4E0E;&#x975E;&#x516C;&#x5E73;&#x9501;"><i class="fa fa-link" aria-hidden="true"></i></a>&#x516C;&#x5E73;&#x9501;&#x4E0E;&#x975E;&#x516C;&#x5E73;&#x9501;</h3>
<p>&#x516C;&#x5E73;&#x9501;&#x4E0E;&#x975E;&#x516C;&#x5E73;&#x9501;&#x7684;&#x533A;&#x522B;&#x5728;&#x4E8E;&#x83B7;&#x53D6;&#x9501;&#x7684;&#x65F6;&#x5019;&#x662F;&#x5426;&#x6309;&#x7167;FIFO&#x7684;&#x987A;&#x5E8F;&#x6765;&#x3002;&#x91CA;&#x653E;&#x9501;&#x4E0D;&#x5B58;&#x5728;&#x516C;&#x5E73;&#x6027;&#x548C;&#x975E;&#x516C;&#x5E73;&#x6027;&#xFF0C;&#x4E0A;&#x9762;&#x4EE5;&#x975E;&#x516C;&#x5E73;&#x9501;&#x4E3A;&#x4F8B;&#xFF0C;&#x4E0B;&#x9762;&#x6211;&#x4EEC;&#x6765;&#x770B;&#x770B;&#x516C;&#x5E73;&#x9501;&#x7684;tryAcquire(int arg)&#xFF1A;</p>
<pre><code class="lang-java">    <span class="hljs-function"><span class="hljs-keyword">protected</span> <span class="hljs-keyword">final</span> <span class="hljs-keyword">boolean</span> <span class="hljs-title">tryAcquire</span><span class="hljs-params">(<span class="hljs-keyword">int</span> acquires)</span> </span>{
        <span class="hljs-keyword">final</span> Thread current = Thread.currentThread();
        <span class="hljs-keyword">int</span> c = getState();
        <span class="hljs-keyword">if</span> (c == <span class="hljs-number">0</span>) {
            <span class="hljs-keyword">if</span> (!hasQueuedPredecessors() &amp;&amp;
                    compareAndSetState(<span class="hljs-number">0</span>, acquires)) {
                setExclusiveOwnerThread(current);
                <span class="hljs-keyword">return</span> <span class="hljs-keyword">true</span>;
            }
        }
        <span class="hljs-keyword">else</span> <span class="hljs-keyword">if</span> (current == getExclusiveOwnerThread()) {
            <span class="hljs-keyword">int</span> nextc = c + acquires;
            <span class="hljs-keyword">if</span> (nextc &lt; <span class="hljs-number">0</span>)
                <span class="hljs-keyword">throw</span> <span class="hljs-keyword">new</span> Error(<span class="hljs-string">&quot;Maximum lock count exceeded&quot;</span>);
            setState(nextc);
            <span class="hljs-keyword">return</span> <span class="hljs-keyword">true</span>;
        }
        <span class="hljs-keyword">return</span> <span class="hljs-keyword">false</span>;
    }
</code></pre>
<p>&#x6BD4;&#x8F83;&#x975E;&#x516C;&#x5E73;&#x9501;&#x548C;&#x516C;&#x5E73;&#x9501;&#x83B7;&#x53D6;&#x540C;&#x6B65;&#x72B6;&#x6001;&#x7684;&#x8FC7;&#x7A0B;&#xFF0C;&#x4F1A;&#x53D1;&#x73B0;&#x4E24;&#x8005;&#x552F;&#x4E00;&#x7684;&#x533A;&#x522B;&#x5C31;&#x5728;&#x4E8E;&#x516C;&#x5E73;&#x9501;&#x5728;&#x83B7;&#x53D6;&#x540C;&#x6B65;&#x72B6;&#x6001;&#x65F6;&#x591A;&#x4E86;&#x4E00;&#x4E2A;&#x9650;&#x5236;&#x6761;&#x4EF6;&#xFF1A;hasQueuedPredecessors()&#xFF0C;&#x5B9A;&#x4E49;&#x5982;&#x4E0B;&#xFF1A;</p>
<pre><code class="lang-java">    <span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-keyword">final</span> <span class="hljs-keyword">boolean</span> <span class="hljs-title">hasQueuedPredecessors</span><span class="hljs-params">()</span> </span>{
        Node t = tail;  <span class="hljs-comment">//&#x5C3E;&#x8282;&#x70B9;</span>
        Node h = head;  <span class="hljs-comment">//&#x5934;&#x8282;&#x70B9;</span>
        Node s;

        <span class="hljs-comment">//&#x5934;&#x8282;&#x70B9; != &#x5C3E;&#x8282;&#x70B9;</span>
        <span class="hljs-comment">//&#x540C;&#x6B65;&#x961F;&#x5217;&#x7B2C;&#x4E00;&#x4E2A;&#x8282;&#x70B9;&#x4E0D;&#x4E3A;null</span>
        <span class="hljs-comment">//&#x5F53;&#x524D;&#x7EBF;&#x7A0B;&#x662F;&#x540C;&#x6B65;&#x961F;&#x5217;&#x7B2C;&#x4E00;&#x4E2A;&#x8282;&#x70B9;</span>
        <span class="hljs-keyword">return</span> h != t &amp;&amp;
                ((s = h.next) == <span class="hljs-keyword">null</span> || s.thread != Thread.currentThread());
    }
</code></pre>
<p>&#x8BE5;&#x65B9;&#x6CD5;&#x4E3B;&#x8981;&#x505A;&#x4E00;&#x4EF6;&#x4E8B;&#x60C5;&#xFF1A;&#x4E3B;&#x8981;&#x662F;&#x5224;&#x65AD;&#x5F53;&#x524D;&#x7EBF;&#x7A0B;&#x662F;&#x5426;&#x4F4D;&#x4E8E;CLH&#x540C;&#x6B65;&#x961F;&#x5217;&#x4E2D;&#x7684;&#x7B2C;&#x4E00;&#x4E2A;&#x3002;&#x5982;&#x679C;&#x662F;&#x5219;&#x8FD4;&#x56DE;true&#xFF0C;&#x5426;&#x5219;&#x8FD4;&#x56DE;false&#x3002;</p>
<h3 id="reentrantlock&#x4E0E;synchronized&#x7684;&#x533A;&#x522B;"><a name="reentrantlock&#x4E0E;synchronized&#x7684;&#x533A;&#x522B;" class="anchor-navigation-ex-anchor" href="#reentrantlock&#x4E0E;synchronized&#x7684;&#x533A;&#x522B;"><i class="fa fa-link" aria-hidden="true"></i></a><a name="reentrantlock&#x4E0E;synchronized&#x7684;&#x533A;&#x522B;" class="plugin-anchor" href="#reentrantlock&#x4E0E;synchronized&#x7684;&#x533A;&#x522B;"><i class="fa fa-link" aria-hidden="true"></i></a>ReentrantLock&#x4E0E;synchronized&#x7684;&#x533A;&#x522B;</h3>
<p>&#x524D;&#x9762;&#x63D0;&#x5230;ReentrantLock&#x63D0;&#x4F9B;&#x4E86;&#x6BD4;synchronized&#x66F4;&#x52A0;&#x7075;&#x6D3B;&#x548C;&#x5F3A;&#x5927;&#x7684;&#x9501;&#x673A;&#x5236;&#xFF0C;&#x90A3;&#x4E48;&#x5B83;&#x7684;&#x7075;&#x6D3B;&#x548C;&#x5F3A;&#x5927;&#x4E4B;&#x5904;&#x5728;&#x54EA;&#x91CC;&#x5462;&#xFF1F;&#x4ED6;&#x4EEC;&#x4E4B;&#x95F4;&#x53C8;&#x6709;&#x4EC0;&#x4E48;&#x76F8;&#x5F02;&#x4E4B;&#x5904;&#x5462;&#xFF1F; &#x9996;&#x5148;&#x4ED6;&#x4EEC;&#x80AF;&#x5B9A;&#x5177;&#x6709;&#x76F8;&#x540C;&#x7684;&#x529F;&#x80FD;&#x548C;&#x5185;&#x5B58;&#x8BED;&#x4E49;&#x3002;</p>
<ol>
<li>&#x4E0E;synchronized&#x76F8;&#x6BD4;&#xFF0C;ReentrantLock&#x63D0;&#x4F9B;&#x4E86;&#x66F4;&#x591A;&#xFF0C;&#x66F4;&#x52A0;&#x5168;&#x9762;&#x7684;&#x529F;&#x80FD;&#xFF0C;&#x5177;&#x5907;&#x66F4;&#x5F3A;&#x7684;&#x6269;&#x5C55;&#x6027;&#x3002;&#x4F8B;&#x5982;&#xFF1A;&#x65F6;&#x95F4;&#x9501;&#x7B49;&#x5019;&#xFF0C;&#x53EF;&#x4E2D;&#x65AD;&#x9501;&#x7B49;&#x5019;&#xFF0C;&#x9501;&#x6295;&#x7968;&#x3002;</li>
<li>ReentrantLock&#x8FD8;&#x63D0;&#x4F9B;&#x4E86;&#x6761;&#x4EF6;Condition&#xFF0C;&#x5BF9;&#x7EBF;&#x7A0B;&#x7684;&#x7B49;&#x5F85;&#x3001;&#x5524;&#x9192;&#x64CD;&#x4F5C;&#x66F4;&#x52A0;&#x8BE6;&#x7EC6;&#x548C;&#x7075;&#x6D3B;&#xFF0C;&#x6240;&#x4EE5;&#x5728;&#x591A;&#x4E2A;&#x6761;&#x4EF6;&#x53D8;&#x91CF;&#x548C;&#x9AD8;&#x5EA6;&#x7ADE;&#x4E89;&#x9501;&#x7684;&#x5730;&#x65B9;&#xFF0C;ReentrantLock&#x66F4;&#x52A0;&#x9002;&#x5408;&#xFF08;&#x4EE5;&#x540E;&#x4F1A;&#x9610;&#x8FF0;Condition&#xFF09;&#x3002;</li>
<li>ReentrantLock&#x63D0;&#x4F9B;&#x4E86;&#x53EF;&#x8F6E;&#x8BE2;&#x7684;&#x9501;&#x8BF7;&#x6C42;&#x3002;&#x5B83;&#x4F1A;&#x5C1D;&#x8BD5;&#x7740;&#x53BB;&#x83B7;&#x53D6;&#x9501;&#xFF0C;&#x5982;&#x679C;&#x6210;&#x529F;&#x5219;&#x7EE7;&#x7EED;&#xFF0C;&#x5426;&#x5219;&#x53EF;&#x4EE5;&#x7B49;&#x5230;&#x4E0B;&#x6B21;&#x8FD0;&#x884C;&#x65F6;&#x5904;&#x7406;&#xFF0C;&#x800C;synchronized&#x5219;&#x4E00;&#x65E6;&#x8FDB;&#x5165;&#x9501;&#x8BF7;&#x6C42;&#x8981;&#x4E48;&#x6210;&#x529F;&#x8981;&#x4E48;&#x963B;&#x585E;&#xFF0C;&#x6240;&#x4EE5;&#x76F8;&#x6BD4;synchronized&#x800C;&#x8A00;&#xFF0C;ReentrantLock&#x4F1A;&#x4E0D;&#x5BB9;&#x6613;&#x4EA7;&#x751F;&#x6B7B;&#x9501;&#x4E9B;&#x3002;</li>
<li>ReentrantLock&#x652F;&#x6301;&#x66F4;&#x52A0;&#x7075;&#x6D3B;&#x7684;&#x540C;&#x6B65;&#x4EE3;&#x7801;&#x5757;&#xFF0C;&#x4F46;&#x662F;&#x4F7F;&#x7528;synchronized&#x65F6;&#xFF0C;&#x53EA;&#x80FD;&#x5728;&#x540C;&#x4E00;&#x4E2A;synchronized&#x5757;&#x7ED3;&#x6784;&#x4E2D;&#x83B7;&#x53D6;&#x548C;&#x91CA;&#x653E;&#x3002;&#x6CE8;&#xFF1A;ReentrantLock&#x7684;&#x9501;&#x91CA;&#x653E;&#x4E00;&#x5B9A;&#x8981;&#x5728;finally&#x4E2D;&#x5904;&#x7406;&#xFF0C;&#x5426;&#x5219;&#x53EF;&#x80FD;&#x4F1A;&#x4EA7;&#x751F;&#x4E25;&#x91CD;&#x7684;&#x540E;&#x679C;&#x3002;</li>
<li>ReentrantLock&#x652F;&#x6301;&#x4E2D;&#x65AD;&#x5904;&#x7406;&#xFF0C;&#x4E14;&#x6027;&#x80FD;&#x8F83;synchronized&#x4F1A;&#x597D;&#x4E9B;&#x3002;</li>
</ol>
<h2 id="&#x4E8C;-condition"><a name="&#x4E8C;-condition" class="anchor-navigation-ex-anchor" href="#&#x4E8C;-condition"><i class="fa fa-link" aria-hidden="true"></i></a><a name="&#x4E8C;-condition" class="plugin-anchor" href="#&#x4E8C;-condition"><i class="fa fa-link" aria-hidden="true"></i></a>&#x4E8C; condition</h2>
<p>&#x5728;&#x6CA1;&#x6709;Lock&#x4E4B;&#x524D;&#xFF0C;&#x6211;&#x4EEC;&#x4F7F;&#x7528;synchronized&#x6765;&#x63A7;&#x5236;&#x540C;&#x6B65;&#xFF0C;&#x914D;&#x5408;Object&#x7684;wait()&#x3001;notify()&#x7CFB;&#x5217;&#x65B9;&#x6CD5;&#x53EF;&#x4EE5;&#x5B9E;&#x73B0;&#x7B49;&#x5F85;/&#x901A;&#x77E5;&#x6A21;&#x5F0F;&#x3002;&#x5728;Java SE5&#x540E;&#xFF0C;Java&#x63D0;&#x4F9B;&#x4E86;Lock&#x63A5;&#x53E3;&#xFF0C;&#x76F8;&#x5BF9;&#x4E8E;Synchronized&#x800C;&#x8A00;&#xFF0C;Lock&#x63D0;&#x4F9B;&#x4E86;&#x6761;&#x4EF6;Condition&#xFF0C;&#x5BF9;&#x7EBF;&#x7A0B;&#x7684;&#x7B49;&#x5F85;&#x3001;&#x5524;&#x9192;&#x64CD;&#x4F5C;&#x66F4;&#x52A0;&#x8BE6;&#x7EC6;&#x548C;&#x7075;&#x6D3B;&#x3002;&#x4E0B;&#x56FE;&#x662F;Condition&#x4E0E;Object&#x7684;&#x76D1;&#x89C6;&#x5668;&#x65B9;&#x6CD5;&#x7684;&#x5BF9;&#x6BD4;&#xFF08;&#x6458;&#x81EA;&#x300A;Java&#x5E76;&#x53D1;&#x7F16;&#x7A0B;&#x7684;&#x827A;&#x672F;&#x300B;&#xFF09;&#xFF1A;</p>
<p><img src="https://gitee.com/zisuu/picture/raw/master/img/20201216194541.png" alt="image-20201216194541383"></p>
<p>Condition&#x63D0;&#x4F9B;&#x4E86;&#x4E00;&#x7CFB;&#x5217;&#x7684;&#x65B9;&#x6CD5;&#x6765;&#x5BF9;&#x963B;&#x585E;&#x548C;&#x5524;&#x9192;&#x7EBF;&#x7A0B;&#xFF1A;</p>
<ol>
<li><strong>await()</strong> &#xFF1A;&#x9020;&#x6210;&#x5F53;&#x524D;&#x7EBF;&#x7A0B;&#x5728;&#x63A5;&#x5230;&#x4FE1;&#x53F7;&#x6216;&#x88AB;&#x4E2D;&#x65AD;&#x4E4B;&#x524D;&#x4E00;&#x76F4;&#x5904;&#x4E8E;&#x7B49;&#x5F85;&#x72B6;&#x6001;&#x3002;</li>
<li><strong>await(long time, TimeUnit unit)</strong> &#xFF1A;&#x9020;&#x6210;&#x5F53;&#x524D;&#x7EBF;&#x7A0B;&#x5728;&#x63A5;&#x5230;&#x4FE1;&#x53F7;&#x3001;&#x88AB;&#x4E2D;&#x65AD;&#x6216;&#x5230;&#x8FBE;&#x6307;&#x5B9A;&#x7B49;&#x5F85;&#x65F6;&#x95F4;&#x4E4B;&#x524D;&#x4E00;&#x76F4;&#x5904;&#x4E8E;&#x7B49;&#x5F85;&#x72B6;&#x6001;&#x3002;</li>
<li><strong>awaitNanos(long nanosTimeout)</strong> &#xFF1A;&#x9020;&#x6210;&#x5F53;&#x524D;&#x7EBF;&#x7A0B;&#x5728;&#x63A5;&#x5230;&#x4FE1;&#x53F7;&#x3001;&#x88AB;&#x4E2D;&#x65AD;&#x6216;&#x5230;&#x8FBE;&#x6307;&#x5B9A;&#x7B49;&#x5F85;&#x65F6;&#x95F4;&#x4E4B;&#x524D;&#x4E00;&#x76F4;&#x5904;&#x4E8E;&#x7B49;&#x5F85;&#x72B6;&#x6001;&#x3002;&#x8FD4;&#x56DE;&#x503C;&#x8868;&#x793A;&#x5269;&#x4F59;&#x65F6;&#x95F4;&#xFF0C;&#x5982;&#x679C;&#x5728;nanosTimesout&#x4E4B;&#x524D;&#x5524;&#x9192;&#xFF0C;&#x90A3;&#x4E48;&#x8FD4;&#x56DE;&#x503C; = nanosTimeout - &#x6D88;&#x8017;&#x65F6;&#x95F4;&#xFF0C;&#x5982;&#x679C;&#x8FD4;&#x56DE;&#x503C; &lt;= 0 ,&#x5219;&#x53EF;&#x4EE5;&#x8BA4;&#x5B9A;&#x5B83;&#x5DF2;&#x7ECF;&#x8D85;&#x65F6;&#x4E86;&#x3002;</li>
<li><strong>awaitUninterruptibly()</strong> &#xFF1A;&#x9020;&#x6210;&#x5F53;&#x524D;&#x7EBF;&#x7A0B;&#x5728;&#x63A5;&#x5230;&#x4FE1;&#x53F7;&#x4E4B;&#x524D;&#x4E00;&#x76F4;&#x5904;&#x4E8E;&#x7B49;&#x5F85;&#x72B6;&#x6001;&#x3002;&#x3010;&#x6CE8;&#x610F;&#xFF1A;&#x8BE5;&#x65B9;&#x6CD5;&#x5BF9;&#x4E2D;&#x65AD;&#x4E0D;&#x654F;&#x611F;&#x3011;&#x3002;</li>
<li><strong>awaitUntil(Date deadline)</strong> &#xFF1A;&#x9020;&#x6210;&#x5F53;&#x524D;&#x7EBF;&#x7A0B;&#x5728;&#x63A5;&#x5230;&#x4FE1;&#x53F7;&#x3001;&#x88AB;&#x4E2D;&#x65AD;&#x6216;&#x5230;&#x8FBE;&#x6307;&#x5B9A;&#x6700;&#x540E;&#x671F;&#x9650;&#x4E4B;&#x524D;&#x4E00;&#x76F4;&#x5904;&#x4E8E;&#x7B49;&#x5F85;&#x72B6;&#x6001;&#x3002;&#x5982;&#x679C;&#x6CA1;&#x6709;&#x5230;&#x6307;&#x5B9A;&#x65F6;&#x95F4;&#x5C31;&#x88AB;&#x901A;&#x77E5;&#xFF0C;&#x5219;&#x8FD4;&#x56DE;true&#xFF0C;&#x5426;&#x5219;&#x8868;&#x793A;&#x5230;&#x4E86;&#x6307;&#x5B9A;&#x65F6;&#x95F4;&#xFF0C;&#x8FD4;&#x56DE;&#x8FD4;&#x56DE;false&#x3002;</li>
<li><strong>signal()</strong> &#xFF1A;&#x5524;&#x9192;&#x4E00;&#x4E2A;&#x7B49;&#x5F85;&#x7EBF;&#x7A0B;&#x3002;&#x8BE5;&#x7EBF;&#x7A0B;&#x4ECE;&#x7B49;&#x5F85;&#x65B9;&#x6CD5;&#x8FD4;&#x56DE;&#x524D;&#x5FC5;&#x987B;&#x83B7;&#x5F97;&#x4E0E;Condition&#x76F8;&#x5173;&#x7684;&#x9501;&#x3002;</li>
<li><strong>signal()All</strong> &#xFF1A;&#x5524;&#x9192;&#x6240;&#x6709;&#x7B49;&#x5F85;&#x7EBF;&#x7A0B;&#x3002;&#x80FD;&#x591F;&#x4ECE;&#x7B49;&#x5F85;&#x65B9;&#x6CD5;&#x8FD4;&#x56DE;&#x7684;&#x7EBF;&#x7A0B;&#x5FC5;&#x987B;&#x83B7;&#x5F97;&#x4E0E;Condition&#x76F8;&#x5173;&#x7684;&#x9501;&#x3002;</li>
</ol>
<p>Condition&#x662F;&#x4E00;&#x79CD;&#x5E7F;&#x4E49;&#x4E0A;&#x7684;&#x6761;&#x4EF6;&#x961F;&#x5217;&#x3002;&#x4ED6;&#x4E3A;&#x7EBF;&#x7A0B;&#x63D0;&#x4F9B;&#x4E86;&#x4E00;&#x79CD;&#x66F4;&#x4E3A;&#x7075;&#x6D3B;&#x7684;&#x7B49;&#x5F85;/&#x901A;&#x77E5;&#x6A21;&#x5F0F;&#xFF0C;&#x7EBF;&#x7A0B;&#x5728;&#x8C03;&#x7528;await&#x65B9;&#x6CD5;&#x540E;&#x6267;&#x884C;&#x6302;&#x8D77;&#x64CD;&#x4F5C;&#xFF0C;&#x76F4;&#x5230;&#x7EBF;&#x7A0B;&#x7B49;&#x5F85;&#x7684;&#x67D0;&#x4E2A;&#x6761;&#x4EF6;&#x4E3A;&#x771F;&#x65F6;&#x624D;&#x4F1A;&#x88AB;&#x5524;&#x9192;&#x3002;Condition&#x5FC5;&#x987B;&#x8981;&#x914D;&#x5408;&#x9501;&#x4E00;&#x8D77;&#x4F7F;&#x7528;&#xFF0C;&#x56E0;&#x4E3A;&#x5BF9;&#x5171;&#x4EAB;&#x72B6;&#x6001;&#x53D8;&#x91CF;&#x7684;&#x8BBF;&#x95EE;&#x53D1;&#x751F;&#x5728;&#x591A;&#x7EBF;&#x7A0B;&#x73AF;&#x5883;&#x4E0B;&#x3002;&#x4E00;&#x4E2A;Condition&#x7684;&#x5B9E;&#x4F8B;&#x5FC5;&#x987B;&#x4E0E;&#x4E00;&#x4E2A;Lock&#x7ED1;&#x5B9A;&#xFF0C;&#x56E0;&#x6B64;Condition&#x4E00;&#x822C;&#x90FD;&#x662F;&#x4F5C;&#x4E3A;Lock&#x7684;&#x5185;&#x90E8;&#x5B9E;&#x73B0;&#x3002;</p>
<p>&#x83B7;&#x53D6;&#x4E00;&#x4E2A;Condition&#x5FC5;&#x987B;&#x8981;&#x901A;&#x8FC7;Lock&#x7684;newCondition()&#x65B9;&#x6CD5;&#x3002;&#x8BE5;&#x65B9;&#x6CD5;&#x5B9A;&#x4E49;&#x5728;&#x63A5;&#x53E3;Lock&#x4E0B;&#x9762;&#xFF0C;&#x8FD4;&#x56DE;&#x7684;&#x7ED3;&#x679C;&#x662F;&#x7ED1;&#x5B9A;&#x5230;&#x6B64; Lock &#x5B9E;&#x4F8B;&#x7684;&#x65B0; Condition &#x5B9E;&#x4F8B;&#x3002;Condition&#x4E3A;&#x4E00;&#x4E2A;&#x63A5;&#x53E3;&#xFF0C;&#x5176;&#x4E0B;&#x4EC5;&#x6709;&#x4E00;&#x4E2A;&#x5B9E;&#x73B0;&#x7C7B;ConditionObject&#xFF0C;&#x7531;&#x4E8E;Condition&#x7684;&#x64CD;&#x4F5C;&#x9700;&#x8981;&#x83B7;&#x53D6;&#x76F8;&#x5173;&#x7684;&#x9501;&#xFF0C;&#x800C;AQS&#x5219;&#x662F;&#x540C;&#x6B65;&#x9501;&#x7684;&#x5B9E;&#x73B0;&#x57FA;&#x7840;&#xFF0C;&#x6240;&#x4EE5;ConditionObject&#x5219;&#x5B9A;&#x4E49;&#x4E3A;AQS&#x7684;&#x5185;&#x90E8;&#x7C7B;&#x3002;&#x5B9A;&#x4E49;&#x5982;&#x4E0B;&#xFF1A;</p>
<pre><code class="lang-java"><span class="hljs-keyword">public</span> <span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">ConditionObject</span> <span class="hljs-keyword">implements</span> <span class="hljs-title">Condition</span>, <span class="hljs-title">java</span>.<span class="hljs-title">io</span>.<span class="hljs-title">Serializable</span> </span>{
}
</code></pre>
<h3 id="&#x7B49;&#x5F85;&#x961F;&#x5217;"><a name="&#x7B49;&#x5F85;&#x961F;&#x5217;" class="anchor-navigation-ex-anchor" href="#&#x7B49;&#x5F85;&#x961F;&#x5217;"><i class="fa fa-link" aria-hidden="true"></i></a><a name="&#x7B49;&#x5F85;&#x961F;&#x5217;" class="plugin-anchor" href="#&#x7B49;&#x5F85;&#x961F;&#x5217;"><i class="fa fa-link" aria-hidden="true"></i></a>&#x7B49;&#x5F85;&#x961F;&#x5217;</h3>
<p>&#x6BCF;&#x4E2A;Condition&#x5BF9;&#x8C61;&#x90FD;&#x5305;&#x542B;&#x7740;&#x4E00;&#x4E2A;FIFO&#x961F;&#x5217;&#xFF0C;&#x8BE5;&#x961F;&#x5217;&#x662F;Condition&#x5BF9;&#x8C61;&#x901A;&#x77E5;/&#x7B49;&#x5F85;&#x529F;&#x80FD;&#x7684;&#x5173;&#x952E;&#x3002;&#x5728;&#x961F;&#x5217;&#x4E2D;&#x6BCF;&#x4E00;&#x4E2A;&#x8282;&#x70B9;&#x90FD;&#x5305;&#x542B;&#x7740;&#x4E00;&#x4E2A;&#x7EBF;&#x7A0B;&#x5F15;&#x7528;&#xFF0C;&#x8BE5;&#x7EBF;&#x7A0B;&#x5C31;&#x662F;&#x5728;&#x8BE5; Condition&#x5BF9;&#x8C61;&#x4E0A;&#x7B49;&#x5F85;&#x7684;&#x7EBF;&#x7A0B;&#x3002;&#x6211;&#x4EEC;&#x770B;Condition&#x7684;&#x5B9A;&#x4E49;&#x5C31;&#x660E;&#x767D;&#x4E86;&#xFF1A;</p>
<pre><code class="lang-java"><span class="hljs-keyword">public</span> <span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">ConditionObject</span> <span class="hljs-keyword">implements</span> <span class="hljs-title">Condition</span>, <span class="hljs-title">java</span>.<span class="hljs-title">io</span>.<span class="hljs-title">Serializable</span> </span>{
    <span class="hljs-keyword">private</span> <span class="hljs-keyword">static</span> <span class="hljs-keyword">final</span> <span class="hljs-keyword">long</span> serialVersionUID = <span class="hljs-number">1173984872572414699L</span>;

    <span class="hljs-comment">//&#x5934;&#x8282;&#x70B9;</span>
    <span class="hljs-keyword">private</span> <span class="hljs-keyword">transient</span> Node firstWaiter;
    <span class="hljs-comment">//&#x5C3E;&#x8282;&#x70B9;</span>
    <span class="hljs-keyword">private</span> <span class="hljs-keyword">transient</span> Node lastWaiter;

    <span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-title">ConditionObject</span><span class="hljs-params">()</span> </span>{
    }

    <span class="hljs-comment">/** &#x7701;&#x7565;&#x65B9;&#x6CD5; **/</span>
}
</code></pre>
<p>&#x4ECE;&#x4E0A;&#x9762;&#x4EE3;&#x7801;&#x53EF;&#x4EE5;&#x770B;&#x51FA;Condition&#x62E5;&#x6709;&#x9996;&#x8282;&#x70B9;&#xFF08;firstWaiter&#xFF09;&#xFF0C;&#x5C3E;&#x8282;&#x70B9;&#xFF08;lastWaiter&#xFF09;&#x3002;&#x5F53;&#x524D;&#x7EBF;&#x7A0B;&#x8C03;&#x7528;await()&#x65B9;&#x6CD5;&#xFF0C;&#x5C06;&#x4F1A;&#x4EE5;&#x5F53;&#x524D;&#x7EBF;&#x7A0B;&#x6784;&#x9020;&#x6210;&#x4E00;&#x4E2A;&#x8282;&#x70B9;&#xFF08;Node&#xFF09;&#xFF0C;&#x5E76;&#x5C06;&#x8282;&#x70B9;&#x52A0;&#x5165;&#x5230;&#x8BE5;&#x961F;&#x5217;&#x7684;&#x5C3E;&#x90E8;&#x3002;&#x7ED3;&#x6784;&#x5982;&#x4E0B;&#xFF1A;</p>
<p><img src="https://gitee.com/zisuu/picture/raw/master/img/20201216194718.png" alt="image-20201216194718491"></p>
<p>Node&#x91CC;&#x9762;&#x5305;&#x542B;&#x4E86;&#x5F53;&#x524D;&#x7EBF;&#x7A0B;&#x7684;&#x5F15;&#x7528;&#x3002;Node&#x5B9A;&#x4E49;&#x4E0E;AQS&#x7684;CLH&#x540C;&#x6B65;&#x961F;&#x5217;&#x7684;&#x8282;&#x70B9;&#x4F7F;&#x7528;&#x7684;&#x90FD;&#x662F;&#x540C;&#x4E00;&#x4E2A;&#x7C7B;&#xFF08;AbstractQueuedSynchronized.Node&#x9759;&#x6001;&#x5185;&#x90E8;&#x7C7B;&#xFF09;&#x3002;</p>
<p>Condition&#x7684;&#x961F;&#x5217;&#x7ED3;&#x6784;&#x6BD4;CLH&#x540C;&#x6B65;&#x961F;&#x5217;&#x7684;&#x7ED3;&#x6784;&#x7B80;&#x5355;&#x4E9B;&#xFF0C;&#x65B0;&#x589E;&#x8FC7;&#x7A0B;&#x8F83;&#x4E3A;&#x7B80;&#x5355;&#x53EA;&#x9700;&#x8981;&#x5C06;&#x539F;&#x5C3E;&#x8282;&#x70B9;&#x7684;nextWaiter&#x6307;&#x5411;&#x65B0;&#x589E;&#x8282;&#x70B9;&#xFF0C;&#x7136;&#x540E;&#x66F4;&#x65B0;lastWaiter&#x5373;&#x53EF;&#x3002;</p>
<h3 id="&#x7B49;&#x5F85;"><a name="&#x7B49;&#x5F85;" class="anchor-navigation-ex-anchor" href="#&#x7B49;&#x5F85;"><i class="fa fa-link" aria-hidden="true"></i></a><a name="&#x7B49;&#x5F85;" class="plugin-anchor" href="#&#x7B49;&#x5F85;"><i class="fa fa-link" aria-hidden="true"></i></a>&#x7B49;&#x5F85;</h3>
<p>&#x8C03;&#x7528;Condition&#x7684;await()&#x65B9;&#x6CD5;&#x4F1A;&#x4F7F;&#x5F53;&#x524D;&#x7EBF;&#x7A0B;&#x8FDB;&#x5165;&#x7B49;&#x5F85;&#x72B6;&#x6001;&#xFF0C;&#x540C;&#x65F6;&#x4F1A;&#x52A0;&#x5165;&#x5230;Condition&#x7B49;&#x5F85;&#x961F;&#x5217;&#x540C;&#x65F6;&#x91CA;&#x653E;&#x9501;&#x3002;&#x5F53;&#x4ECE;await()&#x65B9;&#x6CD5;&#x8FD4;&#x56DE;&#x65F6;&#xFF0C;&#x5F53;&#x524D;&#x7EBF;&#x7A0B;&#x4E00;&#x5B9A;&#x662F;&#x83B7;&#x53D6;&#x4E86;Condition&#x76F8;&#x5173;&#x8FDE;&#x7684;&#x9501;&#x3002;</p>
<pre><code class="lang-java">    <span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-keyword">final</span> <span class="hljs-keyword">void</span> <span class="hljs-title">await</span><span class="hljs-params">()</span> <span class="hljs-keyword">throws</span> InterruptedException </span>{
        <span class="hljs-comment">// &#x5F53;&#x524D;&#x7EBF;&#x7A0B;&#x4E2D;&#x65AD;</span>
        <span class="hljs-keyword">if</span> (Thread.interrupted())
            <span class="hljs-keyword">throw</span> <span class="hljs-keyword">new</span> InterruptedException();
        <span class="hljs-comment">//&#x5F53;&#x524D;&#x7EBF;&#x7A0B;&#x52A0;&#x5165;&#x7B49;&#x5F85;&#x961F;&#x5217;</span>
        Node node = addConditionWaiter();
        <span class="hljs-comment">//&#x91CA;&#x653E;&#x9501;</span>
        <span class="hljs-keyword">long</span> savedState = fullyRelease(node);
        <span class="hljs-keyword">int</span> interruptMode = <span class="hljs-number">0</span>;
        <span class="hljs-comment">/**
         * &#x68C0;&#x6D4B;&#x6B64;&#x8282;&#x70B9;&#x7684;&#x7EBF;&#x7A0B;&#x662F;&#x5426;&#x5728;&#x540C;&#x6B65;&#x961F;&#x4E0A;&#xFF0C;&#x5982;&#x679C;&#x4E0D;&#x5728;&#xFF0C;&#x5219;&#x8BF4;&#x660E;&#x8BE5;&#x7EBF;&#x7A0B;&#x8FD8;&#x4E0D;&#x5177;&#x5907;&#x7ADE;&#x4E89;&#x9501;&#x7684;&#x8D44;&#x683C;&#xFF0C;&#x5219;&#x7EE7;&#x7EED;&#x7B49;&#x5F85;
         * &#x76F4;&#x5230;&#x68C0;&#x6D4B;&#x5230;&#x6B64;&#x8282;&#x70B9;&#x5728;&#x540C;&#x6B65;&#x961F;&#x5217;&#x4E0A;
         */</span>
        <span class="hljs-keyword">while</span> (!isOnSyncQueue(node)) {
            <span class="hljs-comment">//&#x7EBF;&#x7A0B;&#x6302;&#x8D77;</span>
            LockSupport.park(<span class="hljs-keyword">this</span>);
            <span class="hljs-comment">//&#x5982;&#x679C;&#x5DF2;&#x7ECF;&#x4E2D;&#x65AD;&#x4E86;&#xFF0C;&#x5219;&#x9000;&#x51FA;</span>
            <span class="hljs-keyword">if</span> ((interruptMode = checkInterruptWhileWaiting(node)) != <span class="hljs-number">0</span>)
                <span class="hljs-keyword">break</span>;
        }
        <span class="hljs-comment">//&#x7ADE;&#x4E89;&#x540C;&#x6B65;&#x72B6;&#x6001;</span>
        <span class="hljs-keyword">if</span> (acquireQueued(node, savedState) &amp;&amp; interruptMode != THROW_IE)
            interruptMode = REINTERRUPT;
        <span class="hljs-comment">//&#x6E05;&#x7406;&#x4E0B;&#x6761;&#x4EF6;&#x961F;&#x5217;&#x4E2D;&#x7684;&#x4E0D;&#x662F;&#x5728;&#x7B49;&#x5F85;&#x6761;&#x4EF6;&#x7684;&#x8282;&#x70B9;</span>
        <span class="hljs-keyword">if</span> (node.nextWaiter != <span class="hljs-keyword">null</span>) <span class="hljs-comment">// clean up if cancelled</span>
            unlinkCancelledWaiters();
        <span class="hljs-keyword">if</span> (interruptMode != <span class="hljs-number">0</span>)
            reportInterruptAfterWait(interruptMode);
    }
</code></pre>
<p>&#x6B64;&#x6BB5;&#x4EE3;&#x7801;&#x7684;&#x903B;&#x8F91;&#x662F;&#xFF1A;&#x9996;&#x5148;&#x5C06;&#x5F53;&#x524D;&#x7EBF;&#x7A0B;&#x65B0;&#x5EFA;&#x4E00;&#x4E2A;&#x8282;&#x70B9;&#x540C;&#x65F6;&#x52A0;&#x5165;&#x5230;&#x6761;&#x4EF6;&#x961F;&#x5217;&#x4E2D;&#xFF0C;&#x7136;&#x540E;&#x91CA;&#x653E;&#x5F53;&#x524D;&#x7EBF;&#x7A0B;&#x6301;&#x6709;&#x7684;&#x540C;&#x6B65;&#x72B6;&#x6001;&#x3002;&#x7136;&#x540E;&#x5219;&#x662F;&#x4E0D;&#x65AD;&#x68C0;&#x6D4B;&#x8BE5;&#x8282;&#x70B9;&#x4EE3;&#x8868;&#x7684;&#x7EBF;&#x7A0B;&#x91CA;&#x653E;&#x51FA;&#x73B0;&#x5728;CLH&#x540C;&#x6B65;&#x961F;&#x5217;&#x4E2D;&#xFF08;&#x6536;&#x5230;signal&#x4FE1;&#x53F7;&#x4E4B;&#x540E;&#x5C31;&#x4F1A;&#x5728;AQS&#x961F;&#x5217;&#x4E2D;&#x68C0;&#x6D4B;&#x5230;&#xFF09;&#xFF0C;&#x5982;&#x679C;&#x4E0D;&#x5B58;&#x5728;&#x5219;&#x4E00;&#x76F4;&#x6302;&#x8D77;&#xFF0C;&#x5426;&#x5219;&#x53C2;&#x4E0E;&#x7ADE;&#x4E89;&#x540C;&#x6B65;&#x72B6;&#x6001;&#x3002;</p>
<p>&#x52A0;&#x5165;&#x6761;&#x4EF6;&#x961F;&#x5217;&#xFF08;addConditionWaiter()&#xFF09;&#x6E90;&#x7801;&#x5982;&#x4E0B;&#xFF1A;</p>
<pre><code class="lang-java">    <span class="hljs-function"><span class="hljs-keyword">private</span> Node <span class="hljs-title">addConditionWaiter</span><span class="hljs-params">()</span> </span>{
        Node t = lastWaiter;    <span class="hljs-comment">//&#x5C3E;&#x8282;&#x70B9;</span>
        <span class="hljs-comment">//Node&#x7684;&#x8282;&#x70B9;&#x72B6;&#x6001;&#x5982;&#x679C;&#x4E0D;&#x4E3A;CONDITION&#xFF0C;&#x5219;&#x8868;&#x793A;&#x8BE5;&#x8282;&#x70B9;&#x4E0D;&#x5904;&#x4E8E;&#x7B49;&#x5F85;&#x72B6;&#x6001;&#xFF0C;&#x9700;&#x8981;&#x6E05;&#x9664;&#x8282;&#x70B9;</span>
        <span class="hljs-keyword">if</span> (t != <span class="hljs-keyword">null</span> &amp;&amp; t.waitStatus != Node.CONDITION) {
            <span class="hljs-comment">//&#x6E05;&#x9664;&#x6761;&#x4EF6;&#x961F;&#x5217;&#x4E2D;&#x6240;&#x6709;&#x72B6;&#x6001;&#x4E0D;&#x4E3A;Condition&#x7684;&#x8282;&#x70B9;</span>
            unlinkCancelledWaiters();
            t = lastWaiter;
        }
        <span class="hljs-comment">//&#x5F53;&#x524D;&#x7EBF;&#x7A0B;&#x65B0;&#x5EFA;&#x8282;&#x70B9;&#xFF0C;&#x72B6;&#x6001;CONDITION</span>
        Node node = <span class="hljs-keyword">new</span> Node(Thread.currentThread(), Node.CONDITION);
        <span class="hljs-comment">/**
         * &#x5C06;&#x8BE5;&#x8282;&#x70B9;&#x52A0;&#x5165;&#x5230;&#x6761;&#x4EF6;&#x961F;&#x5217;&#x4E2D;&#x6700;&#x540E;&#x4E00;&#x4E2A;&#x4F4D;&#x7F6E;
         */</span>
        <span class="hljs-keyword">if</span> (t == <span class="hljs-keyword">null</span>)
            firstWaiter = node;
        <span class="hljs-keyword">else</span>
            t.nextWaiter = node;
        lastWaiter = node;
        <span class="hljs-keyword">return</span> node;
    }
</code></pre>
<p>&#x8BE5;&#x65B9;&#x6CD5;&#x4E3B;&#x8981;&#x662F;&#x5C06;&#x5F53;&#x524D;&#x7EBF;&#x7A0B;&#x52A0;&#x5165;&#x5230;Condition&#x6761;&#x4EF6;&#x961F;&#x5217;&#x4E2D;&#x3002;&#x5F53;&#x7136;&#x5728;&#x52A0;&#x5165;&#x5230;&#x5C3E;&#x8282;&#x70B9;&#x4E4B;&#x524D;&#x4F1A;&#x6E05;&#x695A;&#x6240;&#x6709;&#x72B6;&#x6001;&#x4E0D;&#x4E3A;Condition&#x7684;&#x8282;&#x70B9;&#x3002;</p>
<p>fullyRelease(Node node)&#xFF0C;&#x8D1F;&#x8D23;&#x91CA;&#x653E;&#x8BE5;&#x7EBF;&#x7A0B;&#x6301;&#x6709;&#x7684;&#x9501;&#x3002;</p>
<pre><code class="lang-java">    <span class="hljs-function"><span class="hljs-keyword">final</span> <span class="hljs-keyword">long</span> <span class="hljs-title">fullyRelease</span><span class="hljs-params">(Node node)</span> </span>{
        <span class="hljs-keyword">boolean</span> failed = <span class="hljs-keyword">true</span>;
        <span class="hljs-keyword">try</span> {
            <span class="hljs-comment">//&#x8282;&#x70B9;&#x72B6;&#x6001;--&#x5176;&#x5B9E;&#x5C31;&#x662F;&#x6301;&#x6709;&#x9501;&#x7684;&#x6570;&#x91CF;</span>
            <span class="hljs-keyword">long</span> savedState = getState();
            <span class="hljs-comment">//&#x91CA;&#x653E;&#x9501;</span>
            <span class="hljs-keyword">if</span> (release(savedState)) {
                failed = <span class="hljs-keyword">false</span>;
                <span class="hljs-keyword">return</span> savedState;
            } <span class="hljs-keyword">else</span> {
                <span class="hljs-keyword">throw</span> <span class="hljs-keyword">new</span> IllegalMonitorStateException();
            }
        } <span class="hljs-keyword">finally</span> {
            <span class="hljs-keyword">if</span> (failed)
                node.waitStatus = Node.CANCELLED;
        }
    }
</code></pre>
<p>isOnSyncQueue(Node node)&#xFF1A;&#x5982;&#x679C;&#x4E00;&#x4E2A;&#x8282;&#x70B9;&#x521A;&#x5F00;&#x59CB;&#x5728;&#x6761;&#x4EF6;&#x961F;&#x5217;&#x4E0A;&#xFF0C;&#x73B0;&#x5728;&#x5728;&#x540C;&#x6B65;&#x961F;&#x5217;&#x4E0A;&#x83B7;&#x53D6;&#x9501;&#x5219;&#x8FD4;&#x56DE;true</p>
<pre><code class="lang-java">    <span class="hljs-function"><span class="hljs-keyword">final</span> <span class="hljs-keyword">boolean</span> <span class="hljs-title">isOnSyncQueue</span><span class="hljs-params">(Node node)</span> </span>{
        <span class="hljs-comment">//&#x72B6;&#x6001;&#x4E3A;Condition&#xFF0C;&#x83B7;&#x53D6;&#x524D;&#x9A71;&#x8282;&#x70B9;&#x4E3A;null&#xFF0C;&#x8FD4;&#x56DE;false</span>
        <span class="hljs-keyword">if</span> (node.waitStatus == Node.CONDITION || node.prev == <span class="hljs-keyword">null</span>)
            <span class="hljs-keyword">return</span> <span class="hljs-keyword">false</span>;
        <span class="hljs-comment">//&#x540E;&#x7EE7;&#x8282;&#x70B9;&#x4E0D;&#x4E3A;null&#xFF0C;&#x80AF;&#x5B9A;&#x5728;CLH&#x540C;&#x6B65;&#x961F;&#x5217;&#x4E2D;</span>
        <span class="hljs-keyword">if</span> (node.next != <span class="hljs-keyword">null</span>)
            <span class="hljs-keyword">return</span> <span class="hljs-keyword">true</span>;

        <span class="hljs-keyword">return</span> findNodeFromTail(node);
    }
</code></pre>
<p>unlinkCancelledWaiters()&#xFF1A;&#x8D1F;&#x8D23;&#x5C06;&#x6761;&#x4EF6;&#x961F;&#x5217;&#x4E2D;&#x72B6;&#x6001;&#x4E0D;&#x4E3A;Condition&#x7684;&#x8282;&#x70B9;&#x5220;&#x9664;</p>
<pre><code class="lang-java">        <span class="hljs-function"><span class="hljs-keyword">private</span> <span class="hljs-keyword">void</span> <span class="hljs-title">unlinkCancelledWaiters</span><span class="hljs-params">()</span> </span>{
            Node t = firstWaiter;
            Node trail = <span class="hljs-keyword">null</span>;
            <span class="hljs-keyword">while</span> (t != <span class="hljs-keyword">null</span>) {
                Node next = t.nextWaiter;
                <span class="hljs-keyword">if</span> (t.waitStatus != Node.CONDITION) {
                    t.nextWaiter = <span class="hljs-keyword">null</span>;
                    <span class="hljs-keyword">if</span> (trail == <span class="hljs-keyword">null</span>)
                        firstWaiter = next;
                    <span class="hljs-keyword">else</span>
                        trail.nextWaiter = next;
                    <span class="hljs-keyword">if</span> (next == <span class="hljs-keyword">null</span>)
                        lastWaiter = trail;
                }
                <span class="hljs-keyword">else</span>
                    trail = t;
                t = next;
            }
        }
</code></pre>
<h3 id="&#x901A;&#x77E5;"><a name="&#x901A;&#x77E5;" class="anchor-navigation-ex-anchor" href="#&#x901A;&#x77E5;"><i class="fa fa-link" aria-hidden="true"></i></a><a name="&#x901A;&#x77E5;" class="plugin-anchor" href="#&#x901A;&#x77E5;"><i class="fa fa-link" aria-hidden="true"></i></a>&#x901A;&#x77E5;</h3>
<p>&#x8C03;&#x7528;Condition&#x7684;signal()&#x65B9;&#x6CD5;&#xFF0C;&#x5C06;&#x4F1A;&#x5524;&#x9192;&#x5728;&#x7B49;&#x5F85;&#x961F;&#x5217;&#x4E2D;&#x7B49;&#x5F85;&#x6700;&#x957F;&#x65F6;&#x95F4;&#x7684;&#x8282;&#x70B9;&#xFF08;&#x6761;&#x4EF6;&#x961F;&#x5217;&#x91CC;&#x7684;&#x9996;&#x8282;&#x70B9;&#xFF09;&#xFF0C;&#x5728;&#x5524;&#x9192;&#x8282;&#x70B9;&#x524D;&#xFF0C;&#x4F1A;&#x5C06;&#x8282;&#x70B9;&#x79FB;&#x5230;CLH&#x540C;&#x6B65;&#x961F;&#x5217;&#x4E2D;&#x3002;</p>
<pre><code class="lang-java">    <span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-keyword">final</span> <span class="hljs-keyword">void</span> <span class="hljs-title">signal</span><span class="hljs-params">()</span> </span>{
        <span class="hljs-comment">//&#x68C0;&#x6D4B;&#x5F53;&#x524D;&#x7EBF;&#x7A0B;&#x662F;&#x5426;&#x4E3A;&#x62E5;&#x6709;&#x9501;&#x7684;&#x72EC;</span>
        <span class="hljs-keyword">if</span> (!isHeldExclusively())
            <span class="hljs-keyword">throw</span> <span class="hljs-keyword">new</span> IllegalMonitorStateException();
        <span class="hljs-comment">//&#x5934;&#x8282;&#x70B9;&#xFF0C;&#x5524;&#x9192;&#x6761;&#x4EF6;&#x961F;&#x5217;&#x4E2D;&#x7684;&#x7B2C;&#x4E00;&#x4E2A;&#x8282;&#x70B9;</span>
        Node first = firstWaiter;
        <span class="hljs-keyword">if</span> (first != <span class="hljs-keyword">null</span>)
            doSignal(first);    <span class="hljs-comment">//&#x5524;&#x9192;</span>
    }
</code></pre>
<p>&#x8BE5;&#x65B9;&#x6CD5;&#x9996;&#x5148;&#x4F1A;&#x5224;&#x65AD;&#x5F53;&#x524D;&#x7EBF;&#x7A0B;&#x662F;&#x5426;&#x5DF2;&#x7ECF;&#x83B7;&#x5F97;&#x4E86;&#x9501;&#xFF0C;&#x8FD9;&#x662F;&#x524D;&#x7F6E;&#x6761;&#x4EF6;&#x3002;&#x7136;&#x540E;&#x5524;&#x9192;&#x6761;&#x4EF6;&#x961F;&#x5217;&#x4E2D;&#x7684;&#x5934;&#x8282;&#x70B9;&#x3002;</p>
<p>doSignal(Node first)&#xFF1A;&#x5524;&#x9192;&#x5934;&#x8282;&#x70B9;</p>
<pre><code class="lang-java">    <span class="hljs-function"><span class="hljs-keyword">private</span> <span class="hljs-keyword">void</span> <span class="hljs-title">doSignal</span><span class="hljs-params">(Node first)</span> </span>{
        do {
            <span class="hljs-comment">//&#x4FEE;&#x6539;&#x5934;&#x7ED3;&#x70B9;&#xFF0C;&#x5B8C;&#x6210;&#x65E7;&#x5934;&#x7ED3;&#x70B9;&#x7684;&#x79FB;&#x51FA;&#x5DE5;&#x4F5C;</span>
            <span class="hljs-keyword">if</span> ( (firstWaiter = first.nextWaiter) == <span class="hljs-keyword">null</span>)
                lastWaiter = <span class="hljs-keyword">null</span>;
            first.nextWaiter = <span class="hljs-keyword">null</span>;
        } <span class="hljs-keyword">while</span> (!transferForSignal(first) &amp;&amp;
                (first = firstWaiter) != <span class="hljs-keyword">null</span>);
    }
</code></pre>
<p>doSignal(Node first)&#x4E3B;&#x8981;&#x662F;&#x505A;&#x4E24;&#x4EF6;&#x4E8B;&#xFF1A;1.&#x4FEE;&#x6539;&#x5934;&#x8282;&#x70B9;&#xFF0C;2.&#x8C03;&#x7528;transferForSignal(Node first) &#x65B9;&#x6CD5;&#x5C06;&#x8282;&#x70B9;&#x79FB;&#x52A8;&#x5230;CLH&#x540C;&#x6B65;&#x961F;&#x5217;&#x4E2D;&#x3002;transferForSignal(Node first)&#x6E90;&#x7801;&#x5982;&#x4E0B;&#xFF1A;</p>
<pre><code class="lang-java">     <span class="hljs-function"><span class="hljs-keyword">final</span> <span class="hljs-keyword">boolean</span> <span class="hljs-title">transferForSignal</span><span class="hljs-params">(Node node)</span> </span>{
        <span class="hljs-comment">//&#x5C06;&#x8BE5;&#x8282;&#x70B9;&#x4ECE;&#x72B6;&#x6001;CONDITION&#x6539;&#x53D8;&#x4E3A;&#x521D;&#x59CB;&#x72B6;&#x6001;0,</span>
        <span class="hljs-keyword">if</span> (!compareAndSetWaitStatus(node, Node.CONDITION, <span class="hljs-number">0</span>))
            <span class="hljs-keyword">return</span> <span class="hljs-keyword">false</span>;

        <span class="hljs-comment">//&#x5C06;&#x8282;&#x70B9;&#x52A0;&#x5165;&#x5230;syn&#x961F;&#x5217;&#x4E2D;&#x53BB;&#xFF0C;&#x8FD4;&#x56DE;&#x7684;&#x662F;syn&#x961F;&#x5217;&#x4E2D;node&#x8282;&#x70B9;&#x524D;&#x9762;&#x7684;&#x4E00;&#x4E2A;&#x8282;&#x70B9;</span>
        Node p = enq(node);
        <span class="hljs-keyword">int</span> ws = p.waitStatus;
        <span class="hljs-comment">//&#x5982;&#x679C;&#x7ED3;&#x70B9;p&#x7684;&#x72B6;&#x6001;&#x4E3A;cancel &#x6216;&#x8005;&#x4FEE;&#x6539;waitStatus&#x5931;&#x8D25;&#xFF0C;&#x5219;&#x76F4;&#x63A5;&#x5524;&#x9192;</span>
        <span class="hljs-keyword">if</span> (ws &gt; <span class="hljs-number">0</span> || !compareAndSetWaitStatus(p, ws, Node.SIGNAL))
            LockSupport.unpark(node.thread);
        <span class="hljs-keyword">return</span> <span class="hljs-keyword">true</span>;
    }
</code></pre>
<p>&#x6574;&#x4E2A;&#x901A;&#x77E5;&#x7684;&#x6D41;&#x7A0B;&#x5982;&#x4E0B;&#xFF1A;</p>
<ol>
<li>&#x5224;&#x65AD;&#x5F53;&#x524D;&#x7EBF;&#x7A0B;&#x662F;&#x5426;&#x5DF2;&#x7ECF;&#x83B7;&#x53D6;&#x4E86;&#x9501;&#xFF0C;&#x5982;&#x679C;&#x6CA1;&#x6709;&#x83B7;&#x53D6;&#x5219;&#x76F4;&#x63A5;&#x629B;&#x51FA;&#x5F02;&#x5E38;&#xFF0C;&#x56E0;&#x4E3A;&#x83B7;&#x53D6;&#x9501;&#x4E3A;&#x901A;&#x77E5;&#x7684;&#x524D;&#x7F6E;&#x6761;&#x4EF6;&#x3002;</li>
<li>&#x5982;&#x679C;&#x7EBF;&#x7A0B;&#x5DF2;&#x7ECF;&#x83B7;&#x53D6;&#x4E86;&#x9501;&#xFF0C;&#x5219;&#x5C06;&#x5524;&#x9192;&#x6761;&#x4EF6;&#x961F;&#x5217;&#x7684;&#x9996;&#x8282;&#x70B9;</li>
<li>&#x5524;&#x9192;&#x9996;&#x8282;&#x70B9;&#x662F;&#x5148;&#x5C06;&#x6761;&#x4EF6;&#x961F;&#x5217;&#x4E2D;&#x7684;&#x5934;&#x8282;&#x70B9;&#x79FB;&#x51FA;&#xFF0C;&#x7136;&#x540E;&#x8C03;&#x7528;AQS&#x7684;enq(Node node)&#x65B9;&#x6CD5;&#x5C06;&#x5176;&#x5B89;&#x5168;&#x5730;&#x79FB;&#x5230;CLH&#x540C;&#x6B65;&#x961F;&#x5217;&#x4E2D;</li>
<li>&#x6700;&#x540E;&#x5224;&#x65AD;&#x5982;&#x679C;&#x8BE5;&#x8282;&#x70B9;&#x7684;&#x540C;&#x6B65;&#x72B6;&#x6001;&#x662F;&#x5426;&#x4E3A;Cancel&#xFF0C;&#x6216;&#x8005;&#x4FEE;&#x6539;&#x72B6;&#x6001;&#x4E3A;Signal&#x5931;&#x8D25;&#x65F6;&#xFF0C;&#x5219;&#x76F4;&#x63A5;&#x8C03;&#x7528;LockSupport&#x5524;&#x9192;&#x8BE5;&#x8282;&#x70B9;&#x7684;&#x7EBF;&#x7A0B;&#x3002;</li>
</ol>
<h3 id="&#x603B;&#x7ED3;"><a name="&#x603B;&#x7ED3;" class="anchor-navigation-ex-anchor" href="#&#x603B;&#x7ED3;"><i class="fa fa-link" aria-hidden="true"></i></a><a name="&#x603B;&#x7ED3;" class="plugin-anchor" href="#&#x603B;&#x7ED3;"><i class="fa fa-link" aria-hidden="true"></i></a>&#x603B;&#x7ED3;</h3>
<p>&#x4E00;&#x4E2A;&#x7EBF;&#x7A0B;&#x83B7;&#x53D6;&#x9501;&#x540E;&#xFF0C;&#x901A;&#x8FC7;&#x8C03;&#x7528;Condition&#x7684;await()&#x65B9;&#x6CD5;&#xFF0C;&#x4F1A;&#x5C06;&#x5F53;&#x524D;&#x7EBF;&#x7A0B;&#x5148;&#x52A0;&#x5165;&#x5230;&#x6761;&#x4EF6;&#x961F;&#x5217;&#x4E2D;&#xFF0C;&#x7136;&#x540E;&#x91CA;&#x653E;&#x9501;&#xFF0C;&#x6700;&#x540E;&#x901A;&#x8FC7;isOnSyncQueue(Node node)&#x65B9;&#x6CD5;&#x4E0D;&#x65AD;&#x81EA;&#x68C0;&#x770B;&#x8282;&#x70B9;&#x662F;&#x5426;&#x5DF2;&#x7ECF;&#x5728;CLH&#x540C;&#x6B65;&#x961F;&#x5217;&#x4E86;&#xFF0C;&#x5982;&#x679C;&#x662F;&#x5219;&#x5C1D;&#x8BD5;&#x83B7;&#x53D6;&#x9501;&#xFF0C;&#x5426;&#x5219;&#x4E00;&#x76F4;&#x6302;&#x8D77;&#x3002;&#x5F53;&#x7EBF;&#x7A0B;&#x8C03;&#x7528;signal()&#x65B9;&#x6CD5;&#x540E;&#xFF0C;&#x7A0B;&#x5E8F;&#x9996;&#x5148;&#x68C0;&#x67E5;&#x5F53;&#x524D;&#x7EBF;&#x7A0B;&#x662F;&#x5426;&#x83B7;&#x53D6;&#x4E86;&#x9501;&#xFF0C;&#x7136;&#x540E;&#x901A;&#x8FC7;doSignal(Node first)&#x65B9;&#x6CD5;&#x5524;&#x9192;CLH&#x540C;&#x6B65;&#x961F;&#x5217;&#x7684;&#x9996;&#x8282;&#x70B9;&#x3002;&#x88AB;&#x5524;&#x9192;&#x7684;&#x7EBF;&#x7A0B;&#xFF0C;&#x5C06;&#x4ECE;await()&#x65B9;&#x6CD5;&#x4E2D;&#x7684;while&#x5FAA;&#x73AF;&#x4E2D;&#x9000;&#x51FA;&#x6765;&#xFF0C;&#x7136;&#x540E;&#x8C03;&#x7528;acquireQueued()&#x65B9;&#x6CD5;&#x7ADE;&#x4E89;&#x540C;&#x6B65;&#x72B6;&#x6001;&#x3002;</p>
<h3 id="condition&#x7684;&#x5E94;&#x7528;"><a name="condition&#x7684;&#x5E94;&#x7528;" class="anchor-navigation-ex-anchor" href="#condition&#x7684;&#x5E94;&#x7528;"><i class="fa fa-link" aria-hidden="true"></i></a><a name="condition&#x7684;&#x5E94;&#x7528;" class="plugin-anchor" href="#condition&#x7684;&#x5E94;&#x7528;"><i class="fa fa-link" aria-hidden="true"></i></a>Condition&#x7684;&#x5E94;&#x7528;</h3>
<p>&#x53EA;&#x77E5;&#x9053;&#x539F;&#x7406;&#xFF0C;&#x5982;&#x679C;&#x4E0D;&#x77E5;&#x9053;&#x4F7F;&#x7528;&#x90A3;&#x5C31;&#x5751;&#x7239;&#x4E86;&#xFF0C;&#x4E0B;&#x9762;&#x662F;&#x7528;Condition&#x5B9E;&#x73B0;&#x7684;&#x751F;&#x4EA7;&#x8005;&#x6D88;&#x8D39;&#x8005;&#x95EE;&#x9898;&#xFF1A;</p>
<pre><code class="lang-java"><span class="hljs-keyword">public</span> <span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">ConditionTest</span> </span>{
    <span class="hljs-keyword">private</span> LinkedList&lt;String&gt; buffer;    <span class="hljs-comment">//&#x5BB9;&#x5668;</span>
    <span class="hljs-keyword">private</span> <span class="hljs-keyword">int</span> maxSize ;           <span class="hljs-comment">//&#x5BB9;&#x5668;&#x6700;&#x5927;</span>
    <span class="hljs-keyword">private</span> Lock lock;
    <span class="hljs-keyword">private</span> Condition fullCondition;
    <span class="hljs-keyword">private</span> Condition notFullCondition;

    ConditionTest(<span class="hljs-keyword">int</span> maxSize){
        <span class="hljs-keyword">this</span>.maxSize = maxSize;
        buffer = <span class="hljs-keyword">new</span> LinkedList&lt;String&gt;();
        lock = <span class="hljs-keyword">new</span> ReentrantLock();
        fullCondition = lock.newCondition();
        notFullCondition = lock.newCondition();
    }

    <span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-keyword">void</span> <span class="hljs-title">set</span><span class="hljs-params">(String string)</span> <span class="hljs-keyword">throws</span> InterruptedException </span>{
        lock.lock();    <span class="hljs-comment">//&#x83B7;&#x53D6;&#x9501;</span>
        <span class="hljs-keyword">try</span> {
            <span class="hljs-keyword">while</span> (maxSize == buffer.size()){
                notFullCondition.await();       <span class="hljs-comment">//&#x6EE1;&#x4E86;&#xFF0C;&#x6DFB;&#x52A0;&#x7684;&#x7EBF;&#x7A0B;&#x8FDB;&#x5165;&#x7B49;&#x5F85;&#x72B6;&#x6001;</span>
            }

            buffer.add(string);
            fullCondition.signal();
        } <span class="hljs-keyword">finally</span> {
            lock.unlock();      <span class="hljs-comment">//&#x8BB0;&#x5F97;&#x91CA;&#x653E;&#x9501;</span>
        }
    }

    <span class="hljs-function"><span class="hljs-keyword">public</span> String <span class="hljs-title">get</span><span class="hljs-params">()</span> <span class="hljs-keyword">throws</span> InterruptedException </span>{
        String string;
        lock.lock();
        <span class="hljs-keyword">try</span> {
            <span class="hljs-keyword">while</span> (buffer.size() == <span class="hljs-number">0</span>){
                fullCondition.await();
            }
            string = buffer.poll();
            notFullCondition.signal();
        } <span class="hljs-keyword">finally</span> {
            lock.unlock();
        }
        <span class="hljs-keyword">return</span> string;
    }
}
</code></pre>
<h2 id="&#x4E09;-reentrantreadwritelock"><a name="&#x4E09;-reentrantreadwritelock" class="anchor-navigation-ex-anchor" href="#&#x4E09;-reentrantreadwritelock"><i class="fa fa-link" aria-hidden="true"></i></a><a name="&#x4E09;-reentrantreadwritelock" class="plugin-anchor" href="#&#x4E09;-reentrantreadwritelock"><i class="fa fa-link" aria-hidden="true"></i></a>&#x4E09; ReentrantReadWriteLock</h2>
<p>&#x91CD;&#x5165;&#x9501;ReentrantLock&#x662F;&#x6392;&#x4ED6;&#x9501;&#xFF0C;&#x6392;&#x4ED6;&#x9501;&#x5728;&#x540C;&#x4E00;&#x65F6;&#x523B;&#x4EC5;&#x6709;&#x4E00;&#x4E2A;&#x7EBF;&#x7A0B;&#x53EF;&#x4EE5;&#x8FDB;&#x884C;&#x8BBF;&#x95EE;&#xFF0C;&#x4F46;&#x662F;&#x5728;&#x5927;&#x591A;&#x6570;&#x573A;&#x666F;&#x4E0B;&#xFF0C;&#x5927;&#x90E8;&#x5206;&#x65F6;&#x95F4;&#x90FD;&#x662F;&#x63D0;&#x4F9B;&#x8BFB;&#x670D;&#x52A1;&#xFF0C;&#x800C;&#x5199;&#x670D;&#x52A1;&#x5360;&#x6709;&#x7684;&#x65F6;&#x95F4;&#x8F83;&#x5C11;&#x3002;&#x7136;&#x800C;&#x8BFB;&#x670D;&#x52A1;&#x4E0D;&#x5B58;&#x5728;&#x6570;&#x636E;&#x7ADE;&#x4E89;&#x95EE;&#x9898;&#xFF0C;&#x5982;&#x679C;&#x4E00;&#x4E2A;&#x7EBF;&#x7A0B;&#x5728;&#x8BFB;&#x65F6;&#x7981;&#x6B62;&#x5176;&#x4ED6;&#x7EBF;&#x7A0B;&#x8BFB;&#x52BF;&#x5FC5;&#x4F1A;&#x5BFC;&#x81F4;&#x6027;&#x80FD;&#x964D;&#x4F4E;&#x3002;&#x6240;&#x4EE5;&#x5C31;&#x63D0;&#x4F9B;&#x4E86;&#x8BFB;&#x5199;&#x9501;&#x3002; &#x8BFB;&#x5199;&#x9501;&#x7EF4;&#x62A4;&#x7740;&#x4E00;&#x5BF9;&#x9501;&#xFF0C;&#x4E00;&#x4E2A;&#x8BFB;&#x9501;&#x548C;&#x4E00;&#x4E2A;&#x5199;&#x9501;&#x3002;&#x901A;&#x8FC7;&#x5206;&#x79BB;&#x8BFB;&#x9501;&#x548C;&#x5199;&#x9501;&#xFF0C;&#x4F7F;&#x5F97;&#x5E76;&#x53D1;&#x6027;&#x6BD4;&#x4E00;&#x822C;&#x7684;&#x6392;&#x4ED6;&#x9501;&#x6709;&#x4E86;&#x8F83;&#x5927;&#x7684;&#x63D0;&#x5347;&#xFF1A;&#x5728;&#x540C;&#x4E00;&#x65F6;&#x95F4;&#x53EF;&#x4EE5;&#x5141;&#x8BB8;&#x591A;&#x4E2A;&#x8BFB;&#x7EBF;&#x7A0B;&#x540C;&#x65F6;&#x8BBF;&#x95EE;&#xFF0C;&#x4F46;&#x662F;&#x5728;&#x5199;&#x7EBF;&#x7A0B;&#x8BBF;&#x95EE;&#x65F6;&#xFF0C;&#x6240;&#x6709;&#x8BFB;&#x7EBF;&#x7A0B;&#x548C;&#x5199;&#x7EBF;&#x7A0B;&#x90FD;&#x4F1A;&#x88AB;&#x963B;&#x585E;&#x3002; &#x8BFB;&#x5199;&#x9501;&#x7684;&#x4E3B;&#x8981;&#x7279;&#x6027;&#xFF1A;</p>
<ol>
<li>&#x516C;&#x5E73;&#x6027;&#xFF1A;&#x652F;&#x6301;&#x516C;&#x5E73;&#x6027;&#x548C;&#x975E;&#x516C;&#x5E73;&#x6027;&#x3002;</li>
<li>&#x91CD;&#x5165;&#x6027;&#xFF1A;&#x652F;&#x6301;&#x91CD;&#x5165;&#x3002;&#x8BFB;&#x5199;&#x9501;&#x6700;&#x591A;&#x652F;&#x6301;65535&#x4E2A;&#x9012;&#x5F52;&#x5199;&#x5165;&#x9501;&#x548C;65535&#x4E2A;&#x9012;&#x5F52;&#x8BFB;&#x53D6;&#x9501;&#x3002;</li>
<li>&#x9501;&#x964D;&#x7EA7;&#xFF1A;&#x9075;&#x5FAA;&#x83B7;&#x53D6;&#x5199;&#x9501;&#x3001;&#x83B7;&#x53D6;&#x8BFB;&#x9501;&#x5728;&#x91CA;&#x653E;&#x5199;&#x9501;&#x7684;&#x6B21;&#x5E8F;&#xFF0C;&#x5199;&#x9501;&#x80FD;&#x591F;&#x964D;&#x7EA7;&#x6210;&#x4E3A;&#x8BFB;&#x9501;</li>
</ol>
<p>&#x8BFB;&#x5199;&#x9501;ReentrantReadWriteLock&#x5B9E;&#x73B0;&#x63A5;&#x53E3;ReadWriteLock&#xFF0C;&#x8BE5;&#x63A5;&#x53E3;&#x7EF4;&#x62A4;&#x4E86;&#x4E00;&#x5BF9;&#x76F8;&#x5173;&#x7684;&#x9501;&#xFF0C;&#x4E00;&#x4E2A;&#x7528;&#x4E8E;&#x53EA;&#x8BFB;&#x64CD;&#x4F5C;&#xFF0C;&#x53E6;&#x4E00;&#x4E2A;&#x7528;&#x4E8E;&#x5199;&#x5165;&#x64CD;&#x4F5C;&#x3002;&#x53EA;&#x8981;&#x6CA1;&#x6709; writer&#xFF0C;&#x8BFB;&#x53D6;&#x9501;&#x53EF;&#x4EE5;&#x7531;&#x591A;&#x4E2A; reader &#x7EBF;&#x7A0B;&#x540C;&#x65F6;&#x4FDD;&#x6301;&#x3002;&#x5199;&#x5165;&#x9501;&#x662F;&#x72EC;&#x5360;&#x7684;&#x3002;</p>
<pre><code class="lang-java"><span class="hljs-keyword">public</span> <span class="hljs-class"><span class="hljs-keyword">interface</span> <span class="hljs-title">ReadWriteLock</span> </span>{
    <span class="hljs-function">Lock <span class="hljs-title">readLock</span><span class="hljs-params">()</span></span>;
    <span class="hljs-function">Lock <span class="hljs-title">writeLock</span><span class="hljs-params">()</span></span>;
}
</code></pre>
<p>ReadWriteLock&#x5B9A;&#x4E49;&#x4E86;&#x4E24;&#x4E2A;&#x65B9;&#x6CD5;&#x3002;readLock()&#x8FD4;&#x56DE;&#x7528;&#x4E8E;&#x8BFB;&#x64CD;&#x4F5C;&#x7684;&#x9501;&#xFF0C;writeLock()&#x8FD4;&#x56DE;&#x7528;&#x4E8E;&#x5199;&#x64CD;&#x4F5C;&#x7684;&#x9501;&#x3002;ReentrantReadWriteLock&#x5B9A;&#x4E49;&#x5982;&#x4E0B;&#xFF1A;</p>
<pre><code class="lang-java">    <span class="hljs-comment">/** &#x5185;&#x90E8;&#x7C7B;  &#x8BFB;&#x9501; */</span>
    <span class="hljs-keyword">private</span> <span class="hljs-keyword">final</span> ReentrantReadWriteLock.ReadLock readerLock;
    <span class="hljs-comment">/** &#x5185;&#x90E8;&#x7C7B;  &#x5199;&#x9501; */</span>
    <span class="hljs-keyword">private</span> <span class="hljs-keyword">final</span> ReentrantReadWriteLock.WriteLock writerLock;

    <span class="hljs-keyword">final</span> Sync sync;

    <span class="hljs-comment">/** &#x4F7F;&#x7528;&#x9ED8;&#x8BA4;&#xFF08;&#x975E;&#x516C;&#x5E73;&#xFF09;&#x7684;&#x6392;&#x5E8F;&#x5C5E;&#x6027;&#x521B;&#x5EFA;&#x4E00;&#x4E2A;&#x65B0;&#x7684; ReentrantReadWriteLock */</span>
    <span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-title">ReentrantReadWriteLock</span><span class="hljs-params">()</span> </span>{
        <span class="hljs-keyword">this</span>(<span class="hljs-keyword">false</span>);
    }

    <span class="hljs-comment">/** &#x4F7F;&#x7528;&#x7ED9;&#x5B9A;&#x7684;&#x516C;&#x5E73;&#x7B56;&#x7565;&#x521B;&#x5EFA;&#x4E00;&#x4E2A;&#x65B0;&#x7684; ReentrantReadWriteLock */</span>
    <span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-title">ReentrantReadWriteLock</span><span class="hljs-params">(<span class="hljs-keyword">boolean</span> fair)</span> </span>{
        sync = fair ? <span class="hljs-keyword">new</span> FairSync() : <span class="hljs-keyword">new</span> NonfairSync();
        readerLock = <span class="hljs-keyword">new</span> ReadLock(<span class="hljs-keyword">this</span>);
        writerLock = <span class="hljs-keyword">new</span> WriteLock(<span class="hljs-keyword">this</span>);
    }

    <span class="hljs-comment">/** &#x8FD4;&#x56DE;&#x7528;&#x4E8E;&#x5199;&#x5165;&#x64CD;&#x4F5C;&#x7684;&#x9501; */</span>
    <span class="hljs-keyword">public</span> ReentrantReadWriteLock.<span class="hljs-function">WriteLock <span class="hljs-title">writeLock</span><span class="hljs-params">()</span> </span>{ <span class="hljs-keyword">return</span> writerLock; }
    <span class="hljs-comment">/** &#x8FD4;&#x56DE;&#x7528;&#x4E8E;&#x8BFB;&#x53D6;&#x64CD;&#x4F5C;&#x7684;&#x9501; */</span>
    <span class="hljs-keyword">public</span> ReentrantReadWriteLock.<span class="hljs-function">ReadLock  <span class="hljs-title">readLock</span><span class="hljs-params">()</span>  </span>{ <span class="hljs-keyword">return</span> readerLock; }

    <span class="hljs-keyword">abstract</span> <span class="hljs-keyword">static</span> <span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">Sync</span> <span class="hljs-keyword">extends</span> <span class="hljs-title">AbstractQueuedSynchronizer</span> </span>{
        <span class="hljs-comment">/**
         * &#x7701;&#x7565;&#x5176;&#x4F59;&#x6E90;&#x4EE3;&#x7801;
         */</span>
    }
    <span class="hljs-keyword">public</span> <span class="hljs-keyword">static</span> <span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">WriteLock</span> <span class="hljs-keyword">implements</span> <span class="hljs-title">Lock</span>, <span class="hljs-title">java</span>.<span class="hljs-title">io</span>.<span class="hljs-title">Serializable</span></span>{
        <span class="hljs-comment">/**
         * &#x7701;&#x7565;&#x5176;&#x4F59;&#x6E90;&#x4EE3;&#x7801;
         */</span>
    }

    <span class="hljs-keyword">public</span> <span class="hljs-keyword">static</span> <span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">ReadLock</span> <span class="hljs-keyword">implements</span> <span class="hljs-title">Lock</span>, <span class="hljs-title">java</span>.<span class="hljs-title">io</span>.<span class="hljs-title">Serializable</span> </span>{
        <span class="hljs-comment">/**
         * &#x7701;&#x7565;&#x5176;&#x4F59;&#x6E90;&#x4EE3;&#x7801;
         */</span>
    }
</code></pre>
<p>ReentrantReadWriteLock&#x4E0E;ReentrantLock&#x4E00;&#x6837;&#xFF0C;&#x5176;&#x9501;&#x4E3B;&#x4F53;&#x4F9D;&#x7136;&#x662F;Sync&#xFF0C;&#x5B83;&#x7684;&#x8BFB;&#x9501;&#x3001;&#x5199;&#x9501;&#x90FD;&#x662F;&#x4F9D;&#x9760;Sync&#x6765;&#x5B9E;&#x73B0;&#x7684;&#x3002;&#x6240;&#x4EE5;ReentrantReadWriteLock&#x5B9E;&#x9645;&#x4E0A;&#x53EA;&#x6709;&#x4E00;&#x4E2A;&#x9501;&#xFF0C;&#x53EA;&#x662F;&#x5728;&#x83B7;&#x53D6;&#x8BFB;&#x53D6;&#x9501;&#x548C;&#x5199;&#x5165;&#x9501;&#x7684;&#x65B9;&#x5F0F;&#x4E0A;&#x4E0D;&#x4E00;&#x6837;&#x800C;&#x5DF2;&#xFF0C;&#x5B83;&#x7684;&#x8BFB;&#x5199;&#x9501;&#x5176;&#x5B9E;&#x5C31;&#x662F;&#x4E24;&#x4E2A;&#x7C7B;&#xFF1A;ReadLock&#x3001;writeLock&#xFF0C;&#x8FD9;&#x4E24;&#x4E2A;&#x7C7B;&#x90FD;&#x662F;lock&#x5B9E;&#x73B0;&#x3002; &#x5728;ReentrantLock&#x4E2D;&#x4F7F;&#x7528;&#x4E00;&#x4E2A;int&#x7C7B;&#x578B;&#x7684;state&#x6765;&#x8868;&#x793A;&#x540C;&#x6B65;&#x72B6;&#x6001;&#xFF0C;&#x8BE5;&#x503C;&#x8868;&#x793A;&#x9501;&#x88AB;&#x4E00;&#x4E2A;&#x7EBF;&#x7A0B;&#x91CD;&#x590D;&#x83B7;&#x53D6;&#x7684;&#x6B21;&#x6570;&#x3002;&#x4F46;&#x662F;&#x8BFB;&#x5199;&#x9501;ReentrantReadWriteLock&#x5185;&#x90E8;&#x7EF4;&#x62A4;&#x7740;&#x4E24;&#x4E2A;&#x4E00;&#x5BF9;&#x9501;&#xFF0C;&#x9700;&#x8981;&#x7528;&#x4E00;&#x4E2A;&#x53D8;&#x91CF;&#x7EF4;&#x62A4;&#x591A;&#x79CD;&#x72B6;&#x6001;&#x3002;<strong>&#x6240;&#x4EE5;&#x8BFB;&#x5199;&#x9501;&#x91C7;&#x7528;&#x201C;&#x6309;&#x4F4D;&#x5207;&#x5272;&#x4F7F;&#x7528;&#x201D;&#x7684;&#x65B9;&#x5F0F;&#x6765;&#x7EF4;&#x62A4;&#x8FD9;&#x4E2A;&#x53D8;&#x91CF;&#xFF0C;&#x5C06;&#x5176;&#x5207;&#x5206;&#x4E3A;&#x4E24;&#x90E8;&#x5206;&#xFF0C;&#x9AD8;16&#x4E3A;&#x8868;&#x793A;&#x8BFB;&#xFF0C;&#x4F4E;16&#x4E3A;&#x8868;&#x793A;&#x5199;&#x3002;&#x5206;&#x5272;&#x4E4B;&#x540E;&#xFF0C;&#x8BFB;&#x5199;&#x9501;&#x662F;&#x5982;&#x4F55;&#x8FC5;&#x901F;&#x786E;&#x5B9A;&#x8BFB;&#x9501;&#x548C;&#x5199;&#x9501;&#x7684;&#x72B6;&#x6001;&#x5462;&#xFF1F;&#x901A;&#x8FC7;&#x4E3A;&#x8FD0;&#x7B97;&#x3002;&#x5047;&#x5982;&#x5F53;&#x524D;&#x540C;&#x6B65;&#x72B6;&#x6001;&#x4E3A;S&#xFF0C;&#x90A3;&#x4E48;&#x5199;&#x72B6;&#x6001;&#x7B49;&#x4E8E; S &amp; 0x0000FFFF&#xFF08;&#x5C06;&#x9AD8;16&#x4F4D;&#x5168;&#x90E8;&#x62B9;&#x53BB;&#xFF09;&#xFF0C;&#x8BFB;&#x72B6;&#x6001;&#x7B49;&#x4E8E;S &gt;&gt;&gt; 16(&#x65E0;&#x7B26;&#x53F7;&#x8865;0&#x53F3;&#x79FB;16&#x4F4D;)&#x3002;&#x4EE3;&#x7801;&#x5982;&#x4E0B;&#xFF1A;</strong></p>
<pre><code class="lang-java">        <span class="hljs-keyword">static</span> <span class="hljs-keyword">final</span> <span class="hljs-keyword">int</span> SHARED_SHIFT   = <span class="hljs-number">16</span>;
        <span class="hljs-keyword">static</span> <span class="hljs-keyword">final</span> <span class="hljs-keyword">int</span> SHARED_UNIT    = (<span class="hljs-number">1</span> &lt;&lt; SHARED_SHIFT);
        <span class="hljs-keyword">static</span> <span class="hljs-keyword">final</span> <span class="hljs-keyword">int</span> MAX_COUNT      = (<span class="hljs-number">1</span> &lt;&lt; SHARED_SHIFT) - <span class="hljs-number">1</span>;
        <span class="hljs-keyword">static</span> <span class="hljs-keyword">final</span> <span class="hljs-keyword">int</span> EXCLUSIVE_MASK = (<span class="hljs-number">1</span> &lt;&lt; SHARED_SHIFT) - <span class="hljs-number">1</span>;

        <span class="hljs-function"><span class="hljs-keyword">static</span> <span class="hljs-keyword">int</span> <span class="hljs-title">sharedCount</span><span class="hljs-params">(<span class="hljs-keyword">int</span> c)</span>    </span>{ <span class="hljs-keyword">return</span> c &gt;&gt;&gt; SHARED_SHIFT; }
        <span class="hljs-function"><span class="hljs-keyword">static</span> <span class="hljs-keyword">int</span> <span class="hljs-title">exclusiveCount</span><span class="hljs-params">(<span class="hljs-keyword">int</span> c)</span> </span>{ <span class="hljs-keyword">return</span> c &amp; EXCLUSIVE_MASK; }
</code></pre>
<h3 id="&#x5199;&#x9501;"><a name="&#x5199;&#x9501;" class="anchor-navigation-ex-anchor" href="#&#x5199;&#x9501;"><i class="fa fa-link" aria-hidden="true"></i></a><a name="&#x5199;&#x9501;" class="plugin-anchor" href="#&#x5199;&#x9501;"><i class="fa fa-link" aria-hidden="true"></i></a>&#x5199;&#x9501;</h3>
<p>&#x5199;&#x9501;&#x5C31;&#x662F;&#x4E00;&#x4E2A;&#x652F;&#x6301;&#x53EF;&#x91CD;&#x5165;&#x7684;&#x6392;&#x4ED6;&#x9501;&#x3002;</p>
<h4 id="&#x5199;&#x9501;&#x7684;&#x83B7;&#x53D6;"><a name="&#x5199;&#x9501;&#x7684;&#x83B7;&#x53D6;" class="anchor-navigation-ex-anchor" href="#&#x5199;&#x9501;&#x7684;&#x83B7;&#x53D6;"><i class="fa fa-link" aria-hidden="true"></i></a><a name="&#x5199;&#x9501;&#x7684;&#x83B7;&#x53D6;" class="plugin-anchor" href="#&#x5199;&#x9501;&#x7684;&#x83B7;&#x53D6;"><i class="fa fa-link" aria-hidden="true"></i></a>&#x5199;&#x9501;&#x7684;&#x83B7;&#x53D6;</h4>
<p>&#x5199;&#x9501;&#x7684;&#x83B7;&#x53D6;&#x6700;&#x7EC8;&#x4F1A;&#x8C03;&#x7528;tryAcquire(int arg)&#xFF0C;&#x8BE5;&#x65B9;&#x6CD5;&#x5728;&#x5185;&#x90E8;&#x7C7B;Sync&#x4E2D;&#x5B9E;&#x73B0;&#xFF1A;</p>
<pre><code class="lang-java">    <span class="hljs-function"><span class="hljs-keyword">protected</span> <span class="hljs-keyword">final</span> <span class="hljs-keyword">boolean</span> <span class="hljs-title">tryAcquire</span><span class="hljs-params">(<span class="hljs-keyword">int</span> acquires)</span> </span>{
        Thread current = Thread.currentThread();
        <span class="hljs-comment">//&#x5F53;&#x524D;&#x9501;&#x4E2A;&#x6570;</span>
        <span class="hljs-keyword">int</span> c = getState();
        <span class="hljs-comment">//&#x5199;&#x9501;</span>
        <span class="hljs-keyword">int</span> w = exclusiveCount(c);
        <span class="hljs-keyword">if</span> (c != <span class="hljs-number">0</span>) {
            <span class="hljs-comment">//c != 0 &amp;&amp; w == 0 &#x8868;&#x793A;&#x5B58;&#x5728;&#x8BFB;&#x9501;</span>
            <span class="hljs-comment">//&#x5F53;&#x524D;&#x7EBF;&#x7A0B;&#x4E0D;&#x662F;&#x5DF2;&#x7ECF;&#x83B7;&#x53D6;&#x5199;&#x9501;&#x7684;&#x7EBF;&#x7A0B;</span>
            <span class="hljs-keyword">if</span> (w == <span class="hljs-number">0</span> || current != getExclusiveOwnerThread())
                <span class="hljs-keyword">return</span> <span class="hljs-keyword">false</span>;
            <span class="hljs-comment">//&#x8D85;&#x51FA;&#x6700;&#x5927;&#x8303;&#x56F4;</span>
            <span class="hljs-keyword">if</span> (w + exclusiveCount(acquires) &gt; MAX_COUNT)
                <span class="hljs-keyword">throw</span> <span class="hljs-keyword">new</span> Error(<span class="hljs-string">&quot;Maximum lock count exceeded&quot;</span>);
            setState(c + acquires);
            <span class="hljs-keyword">return</span> <span class="hljs-keyword">true</span>;
        }
        <span class="hljs-comment">//&#x662F;&#x5426;&#x9700;&#x8981;&#x963B;&#x585E;</span>
        <span class="hljs-keyword">if</span> (writerShouldBlock() ||
                !compareAndSetState(c, c + acquires))
            <span class="hljs-keyword">return</span> <span class="hljs-keyword">false</span>;
        <span class="hljs-comment">//&#x8BBE;&#x7F6E;&#x83B7;&#x53D6;&#x9501;&#x7684;&#x7EBF;&#x7A0B;&#x4E3A;&#x5F53;&#x524D;&#x7EBF;&#x7A0B;</span>
        setExclusiveOwnerThread(current);
        <span class="hljs-keyword">return</span> <span class="hljs-keyword">true</span>;
    }
</code></pre>
<p>&#x8BE5;&#x65B9;&#x6CD5;&#x548C;ReentrantLock&#x7684;tryAcquire(int arg)&#x5927;&#x81F4;&#x4E00;&#x6837;&#xFF0C;&#x5728;&#x5224;&#x65AD;&#x91CD;&#x5165;&#x65F6;&#x589E;&#x52A0;&#x4E86;&#x4E00;&#x9879;&#x6761;&#x4EF6;&#xFF1A;&#x8BFB;&#x9501;&#x662F;&#x5426;&#x5B58;&#x5728;&#x3002;&#x56E0;&#x4E3A;&#x8981;&#x786E;&#x4FDD;&#x5199;&#x9501;&#x7684;&#x64CD;&#x4F5C;&#x5BF9;&#x8BFB;&#x9501;&#x662F;&#x53EF;&#x89C1;&#x7684;&#xFF0C;&#x5982;&#x679C;&#x5728;&#x5B58;&#x5728;&#x8BFB;&#x9501;&#x7684;&#x60C5;&#x51B5;&#x4E0B;&#x5141;&#x8BB8;&#x83B7;&#x53D6;&#x5199;&#x9501;&#xFF0C;&#x90A3;&#x4E48;&#x90A3;&#x4E9B;&#x5DF2;&#x7ECF;&#x83B7;&#x53D6;&#x8BFB;&#x9501;&#x7684;&#x5176;&#x4ED6;&#x7EBF;&#x7A0B;&#x53EF;&#x80FD;&#x5C31;&#x65E0;&#x6CD5;&#x611F;&#x77E5;&#x5F53;&#x524D;&#x5199;&#x7EBF;&#x7A0B;&#x7684;&#x64CD;&#x4F5C;&#x3002;&#x56E0;&#x6B64;&#x53EA;&#x6709;&#x7B49;&#x8BFB;&#x9501;&#x5B8C;&#x5168;&#x91CA;&#x653E;&#x540E;&#xFF0C;&#x5199;&#x9501;&#x624D;&#x80FD;&#x591F;&#x88AB;&#x5F53;&#x524D;&#x7EBF;&#x7A0B;&#x6240;&#x83B7;&#x53D6;&#xFF0C;&#x4E00;&#x65E6;&#x5199;&#x9501;&#x83B7;&#x53D6;&#x4E86;&#xFF0C;&#x6240;&#x6709;&#x5176;&#x4ED6;&#x8BFB;&#x3001;&#x5199;&#x7EBF;&#x7A0B;&#x5747;&#x4F1A;&#x88AB;&#x963B;&#x585E;&#x3002;</p>
<h4 id="&#x5199;&#x9501;&#x7684;&#x91CA;&#x653E;"><a name="&#x5199;&#x9501;&#x7684;&#x91CA;&#x653E;" class="anchor-navigation-ex-anchor" href="#&#x5199;&#x9501;&#x7684;&#x91CA;&#x653E;"><i class="fa fa-link" aria-hidden="true"></i></a><a name="&#x5199;&#x9501;&#x7684;&#x91CA;&#x653E;" class="plugin-anchor" href="#&#x5199;&#x9501;&#x7684;&#x91CA;&#x653E;"><i class="fa fa-link" aria-hidden="true"></i></a>&#x5199;&#x9501;&#x7684;&#x91CA;&#x653E;</h4>
<p>&#x83B7;&#x53D6;&#x4E86;&#x5199;&#x9501;&#x7528;&#x5B8C;&#x4E86;&#x5219;&#x9700;&#x8981;&#x91CA;&#x653E;&#xFF0C;WriteLock&#x63D0;&#x4F9B;&#x4E86;unlock()&#x65B9;&#x6CD5;&#x91CA;&#x653E;&#x5199;&#x9501;&#xFF1A;</p>
<pre><code class="lang-java">    <span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-keyword">void</span> <span class="hljs-title">unlock</span><span class="hljs-params">()</span> </span>{
        sync.release(<span class="hljs-number">1</span>);
    }

    <span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-keyword">final</span> <span class="hljs-keyword">boolean</span> <span class="hljs-title">release</span><span class="hljs-params">(<span class="hljs-keyword">int</span> arg)</span> </span>{
        <span class="hljs-keyword">if</span> (tryRelease(arg)) {
            Node h = head;
            <span class="hljs-keyword">if</span> (h != <span class="hljs-keyword">null</span> &amp;&amp; h.waitStatus != <span class="hljs-number">0</span>)
                unparkSuccessor(h);
            <span class="hljs-keyword">return</span> <span class="hljs-keyword">true</span>;
        }
        <span class="hljs-keyword">return</span> <span class="hljs-keyword">false</span>;
    }
</code></pre>
<p>&#x5199;&#x9501;&#x7684;&#x91CA;&#x653E;&#x6700;&#x7EC8;&#x8FD8;&#x662F;&#x4F1A;&#x8C03;&#x7528;AQS&#x7684;&#x6A21;&#x677F;&#x65B9;&#x6CD5;release(int arg)&#x65B9;&#x6CD5;&#xFF0C;&#x8BE5;&#x65B9;&#x6CD5;&#x9996;&#x5148;&#x8C03;&#x7528;tryRelease(int arg)&#x65B9;&#x6CD5;&#x5C1D;&#x8BD5;&#x91CA;&#x653E;&#x9501;&#xFF0C;tryRelease(int arg)&#x65B9;&#x6CD5;&#x4E3A;&#x8BFB;&#x5199;&#x9501;&#x5185;&#x90E8;&#x7C7B;Sync&#x4E2D;&#x5B9A;&#x4E49;&#x4E86;&#xFF0C;&#x5982;&#x4E0B;&#xFF1A;</p>
<pre><code class="lang-java">    <span class="hljs-function"><span class="hljs-keyword">protected</span> <span class="hljs-keyword">final</span> <span class="hljs-keyword">boolean</span> <span class="hljs-title">tryRelease</span><span class="hljs-params">(<span class="hljs-keyword">int</span> releases)</span> </span>{
        <span class="hljs-comment">//&#x91CA;&#x653E;&#x7684;&#x7EBF;&#x7A0B;&#x4E0D;&#x4E3A;&#x9501;&#x7684;&#x6301;&#x6709;&#x8005;</span>
        <span class="hljs-keyword">if</span> (!isHeldExclusively())
            <span class="hljs-keyword">throw</span> <span class="hljs-keyword">new</span> IllegalMonitorStateException();
        <span class="hljs-keyword">int</span> nextc = getState() - releases;
        <span class="hljs-comment">//&#x82E5;&#x5199;&#x9501;&#x7684;&#x65B0;&#x7EBF;&#x7A0B;&#x6570;&#x4E3A;0&#xFF0C;&#x5219;&#x5C06;&#x9501;&#x7684;&#x6301;&#x6709;&#x8005;&#x8BBE;&#x7F6E;&#x4E3A;null</span>
        <span class="hljs-keyword">boolean</span> free = exclusiveCount(nextc) == <span class="hljs-number">0</span>;
        <span class="hljs-keyword">if</span> (free)
            setExclusiveOwnerThread(<span class="hljs-keyword">null</span>);
        setState(nextc);
        <span class="hljs-keyword">return</span> free;
    }
</code></pre>
<p>&#x5199;&#x9501;&#x91CA;&#x653E;&#x9501;&#x7684;&#x6574;&#x4E2A;&#x8FC7;&#x7A0B;&#x548C;&#x72EC;&#x5360;&#x9501;ReentrantLock&#x76F8;&#x4F3C;&#xFF0C;&#x6BCF;&#x6B21;&#x91CA;&#x653E;&#x5747;&#x662F;&#x51CF;&#x5C11;&#x5199;&#x72B6;&#x6001;&#xFF0C;&#x5F53;&#x5199;&#x72B6;&#x6001;&#x4E3A;0&#x65F6;&#x8868;&#x793A; &#x5199;&#x9501;&#x5DF2;&#x7ECF;&#x5B8C;&#x5168;&#x91CA;&#x653E;&#x4E86;&#xFF0C;&#x4ECE;&#x800C;&#x7B49;&#x5F85;&#x7684;&#x5176;&#x4ED6;&#x7EBF;&#x7A0B;&#x53EF;&#x4EE5;&#x7EE7;&#x7EED;&#x8BBF;&#x95EE;&#x8BFB;&#x5199;&#x9501;&#xFF0C;&#x83B7;&#x53D6;&#x540C;&#x6B65;&#x72B6;&#x6001;&#xFF0C;&#x540C;&#x65F6;&#x6B64;&#x6B21;&#x5199;&#x7EBF;&#x7A0B;&#x7684;&#x4FEE;&#x6539;&#x5BF9;&#x540E;&#x7EED;&#x7684;&#x7EBF;&#x7A0B;&#x53EF;&#x89C1;&#x3002;</p>
<h3 id="&#x8BFB;&#x9501;"><a name="&#x8BFB;&#x9501;" class="anchor-navigation-ex-anchor" href="#&#x8BFB;&#x9501;"><i class="fa fa-link" aria-hidden="true"></i></a><a name="&#x8BFB;&#x9501;" class="plugin-anchor" href="#&#x8BFB;&#x9501;"><i class="fa fa-link" aria-hidden="true"></i></a>&#x8BFB;&#x9501;</h3>
<p>&#x8BFB;&#x9501;&#x4E3A;&#x4E00;&#x4E2A;&#x53EF;&#x91CD;&#x5165;&#x7684;&#x5171;&#x4EAB;&#x9501;&#xFF0C;&#x5B83;&#x80FD;&#x591F;&#x88AB;&#x591A;&#x4E2A;&#x7EBF;&#x7A0B;&#x540C;&#x65F6;&#x6301;&#x6709;&#xFF0C;&#x5728;&#x6CA1;&#x6709;&#x5176;&#x4ED6;&#x5199;&#x7EBF;&#x7A0B;&#x8BBF;&#x95EE;&#x65F6;&#xFF0C;&#x8BFB;&#x9501;&#x603B;&#x662F;&#x6216;&#x83B7;&#x53D6;&#x6210;&#x529F;&#x3002;</p>
<h4 id="&#x8BFB;&#x9501;&#x7684;&#x83B7;&#x53D6;"><a name="&#x8BFB;&#x9501;&#x7684;&#x83B7;&#x53D6;" class="anchor-navigation-ex-anchor" href="#&#x8BFB;&#x9501;&#x7684;&#x83B7;&#x53D6;"><i class="fa fa-link" aria-hidden="true"></i></a><a name="&#x8BFB;&#x9501;&#x7684;&#x83B7;&#x53D6;" class="plugin-anchor" href="#&#x8BFB;&#x9501;&#x7684;&#x83B7;&#x53D6;"><i class="fa fa-link" aria-hidden="true"></i></a>&#x8BFB;&#x9501;&#x7684;&#x83B7;&#x53D6;</h4>
<p>&#x8BFB;&#x9501;&#x7684;&#x83B7;&#x53D6;&#x53EF;&#x4EE5;&#x901A;&#x8FC7;ReadLock&#x7684;lock()&#x65B9;&#x6CD5;&#xFF1A;</p>
<pre><code class="lang-java">        <span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-keyword">void</span> <span class="hljs-title">lock</span><span class="hljs-params">()</span> </span>{
            sync.acquireShared(<span class="hljs-number">1</span>);
        }
</code></pre>
<p>Sync&#x7684;acquireShared(int arg)&#x5B9A;&#x4E49;&#x5728;AQS&#x4E2D;&#xFF1A;</p>
<pre><code class="lang-java">    <span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-keyword">final</span> <span class="hljs-keyword">void</span> <span class="hljs-title">acquireShared</span><span class="hljs-params">(<span class="hljs-keyword">int</span> arg)</span> </span>{
        <span class="hljs-keyword">if</span> (tryAcquireShared(arg) &lt; <span class="hljs-number">0</span>)
            doAcquireShared(arg);
    }
</code></pre>
<p>tryAcqurireShared(int arg)&#x5C1D;&#x8BD5;&#x83B7;&#x53D6;&#x8BFB;&#x540C;&#x6B65;&#x72B6;&#x6001;&#xFF0C;&#x8BE5;&#x65B9;&#x6CD5;&#x4E3B;&#x8981;&#x7528;&#x4E8E;&#x83B7;&#x53D6;&#x5171;&#x4EAB;&#x5F0F;&#x540C;&#x6B65;&#x72B6;&#x6001;&#xFF0C;&#x83B7;&#x53D6;&#x6210;&#x529F;&#x8FD4;&#x56DE; &gt;= 0&#x7684;&#x8FD4;&#x56DE;&#x7ED3;&#x679C;&#xFF0C;&#x5426;&#x5219;&#x8FD4;&#x56DE; &lt; 0 &#x7684;&#x8FD4;&#x56DE;&#x7ED3;&#x679C;&#x3002;</p>
<pre><code class="lang-java">    <span class="hljs-function"><span class="hljs-keyword">protected</span> <span class="hljs-keyword">final</span> <span class="hljs-keyword">int</span> <span class="hljs-title">tryAcquireShared</span><span class="hljs-params">(<span class="hljs-keyword">int</span> unused)</span> </span>{
        <span class="hljs-comment">//&#x5F53;&#x524D;&#x7EBF;&#x7A0B;</span>
        Thread current = Thread.currentThread();
        <span class="hljs-keyword">int</span> c = getState();
        <span class="hljs-comment">//exclusiveCount(c)&#x8BA1;&#x7B97;&#x5199;&#x9501;</span>
        <span class="hljs-comment">//&#x5982;&#x679C;&#x5B58;&#x5728;&#x5199;&#x9501;&#xFF0C;&#x4E14;&#x9501;&#x7684;&#x6301;&#x6709;&#x8005;&#x4E0D;&#x662F;&#x5F53;&#x524D;&#x7EBF;&#x7A0B;&#xFF0C;&#x76F4;&#x63A5;&#x8FD4;&#x56DE;-1</span>
        <span class="hljs-comment">//&#x5B58;&#x5728;&#x9501;&#x964D;&#x7EA7;&#x95EE;&#x9898;&#xFF0C;&#x540E;&#x7EED;&#x9610;&#x8FF0;</span>
        <span class="hljs-keyword">if</span> (exclusiveCount(c) != <span class="hljs-number">0</span> &amp;&amp;
                getExclusiveOwnerThread() != current)
            <span class="hljs-keyword">return</span> -<span class="hljs-number">1</span>;
        <span class="hljs-comment">//&#x8BFB;&#x9501;</span>
        <span class="hljs-keyword">int</span> r = sharedCount(c);

        <span class="hljs-comment">/*
         * readerShouldBlock():&#x8BFB;&#x9501;&#x662F;&#x5426;&#x9700;&#x8981;&#x7B49;&#x5F85;&#xFF08;&#x516C;&#x5E73;&#x9501;&#x539F;&#x5219;&#xFF09;
         * r &lt; MAX_COUNT&#xFF1A;&#x6301;&#x6709;&#x7EBF;&#x7A0B;&#x5C0F;&#x4E8E;&#x6700;&#x5927;&#x6570;&#xFF08;65535&#xFF09;
         * compareAndSetState(c, c + SHARED_UNIT)&#xFF1A;&#x8BBE;&#x7F6E;&#x8BFB;&#x53D6;&#x9501;&#x72B6;&#x6001;
         */</span>
        <span class="hljs-keyword">if</span> (!readerShouldBlock() &amp;&amp;
                r &lt; MAX_COUNT &amp;&amp;
                compareAndSetState(c, c + SHARED_UNIT)) {
            <span class="hljs-comment">/*
             * holdCount&#x90E8;&#x5206;&#x540E;&#x9762;&#x8BB2;&#x89E3;
             */</span>
            <span class="hljs-keyword">if</span> (r == <span class="hljs-number">0</span>) {
                firstReader = current;
                firstReaderHoldCount = <span class="hljs-number">1</span>;
            } <span class="hljs-keyword">else</span> <span class="hljs-keyword">if</span> (firstReader == current) {
                firstReaderHoldCount++;
            } <span class="hljs-keyword">else</span> {
                HoldCounter rh = cachedHoldCounter;
                <span class="hljs-keyword">if</span> (rh == <span class="hljs-keyword">null</span> || rh.tid != getThreadId(current))
                    cachedHoldCounter = rh = readHolds.get();
                <span class="hljs-keyword">else</span> <span class="hljs-keyword">if</span> (rh.count == <span class="hljs-number">0</span>)
                    readHolds.set(rh);
                rh.count++;
            }
            <span class="hljs-keyword">return</span> <span class="hljs-number">1</span>;
        }
        <span class="hljs-keyword">return</span> fullTryAcquireShared(current);
    }
</code></pre>
<p>&#x8BFB;&#x9501;&#x83B7;&#x53D6;&#x7684;&#x8FC7;&#x7A0B;&#x76F8;&#x5BF9;&#x4E8E;&#x72EC;&#x5360;&#x9501;&#x800C;&#x8A00;&#x4F1A;&#x7A0D;&#x5FAE;&#x590D;&#x6742;&#x4E0B;&#xFF0C;&#x6574;&#x4E2A;&#x8FC7;&#x7A0B;&#x5982;&#x4E0B;&#xFF1A;</p>
<ol>
<li>&#x56E0;&#x4E3A;&#x5B58;&#x5728;&#x9501;&#x964D;&#x7EA7;&#x60C5;&#x51B5;&#xFF0C;&#x5982;&#x679C;&#x5B58;&#x5728;&#x5199;&#x9501;&#x4E14;&#x9501;&#x7684;&#x6301;&#x6709;&#x8005;&#x4E0D;&#x662F;&#x5F53;&#x524D;&#x7EBF;&#x7A0B;&#x5219;&#x76F4;&#x63A5;&#x8FD4;&#x56DE;&#x5931;&#x8D25;&#xFF0C;&#x5426;&#x5219;&#x7EE7;&#x7EED;</li>
<li>&#x4F9D;&#x636E;&#x516C;&#x5E73;&#x6027;&#x539F;&#x5219;&#xFF0C;&#x5224;&#x65AD;&#x8BFB;&#x9501;&#x662F;&#x5426;&#x9700;&#x8981;&#x963B;&#x585E;&#xFF0C;&#x8BFB;&#x9501;&#x6301;&#x6709;&#x7EBF;&#x7A0B;&#x6570;&#x5C0F;&#x4E8E;&#x6700;&#x5927;&#x503C;&#xFF08;65535&#xFF09;&#xFF0C;&#x4E14;&#x8BBE;&#x7F6E;&#x9501;&#x72B6;&#x6001;&#x6210;&#x529F;&#xFF0C;&#x6267;&#x884C;&#x4EE5;&#x4E0B;&#x4EE3;&#x7801;&#xFF08;&#x5BF9;&#x4E8E;HoldCounter&#x4E0B;&#x9762;&#x518D;&#x9610;&#x8FF0;&#xFF09;&#xFF0C;&#x5E76;&#x8FD4;&#x56DE;1&#x3002;&#x5982;&#x679C;&#x4E0D;&#x6EE1;&#x8DB3;&#x6539;&#x6761;&#x4EF6;&#xFF0C;&#x6267;&#x884C;fullTryAcquireShared()&#x3002;</li>
</ol>
<pre><code class="lang-java">    <span class="hljs-function"><span class="hljs-keyword">final</span> <span class="hljs-keyword">int</span> <span class="hljs-title">fullTryAcquireShared</span><span class="hljs-params">(Thread current)</span> </span>{
        HoldCounter rh = <span class="hljs-keyword">null</span>;
        <span class="hljs-keyword">for</span> (;;) {
            <span class="hljs-keyword">int</span> c = getState();
            <span class="hljs-comment">//&#x9501;&#x964D;&#x7EA7;</span>
            <span class="hljs-keyword">if</span> (exclusiveCount(c) != <span class="hljs-number">0</span>) {
                <span class="hljs-keyword">if</span> (getExclusiveOwnerThread() != current)
                    <span class="hljs-keyword">return</span> -<span class="hljs-number">1</span>;
            }
            <span class="hljs-comment">//&#x8BFB;&#x9501;&#x9700;&#x8981;&#x963B;&#x585E;</span>
            <span class="hljs-keyword">else</span> <span class="hljs-keyword">if</span> (readerShouldBlock()) {
                <span class="hljs-comment">//&#x5217;&#x5934;&#x4E3A;&#x5F53;&#x524D;&#x7EBF;&#x7A0B;</span>
                <span class="hljs-keyword">if</span> (firstReader == current) {
                }
                <span class="hljs-comment">//HoldCounter&#x540E;&#x9762;&#x8BB2;&#x89E3;</span>
                <span class="hljs-keyword">else</span> {
                    <span class="hljs-keyword">if</span> (rh == <span class="hljs-keyword">null</span>) {
                        rh = cachedHoldCounter;
                        <span class="hljs-keyword">if</span> (rh == <span class="hljs-keyword">null</span> || rh.tid != getThreadId(current)) {
                            rh = readHolds.get();
                            <span class="hljs-keyword">if</span> (rh.count == <span class="hljs-number">0</span>)
                                readHolds.remove();
                        }
                    }
                    <span class="hljs-keyword">if</span> (rh.count == <span class="hljs-number">0</span>)
                        <span class="hljs-keyword">return</span> -<span class="hljs-number">1</span>;
                }
            }
            <span class="hljs-comment">//&#x8BFB;&#x9501;&#x8D85;&#x51FA;&#x6700;&#x5927;&#x8303;&#x56F4;</span>
            <span class="hljs-keyword">if</span> (sharedCount(c) == MAX_COUNT)
                <span class="hljs-keyword">throw</span> <span class="hljs-keyword">new</span> Error(<span class="hljs-string">&quot;Maximum lock count exceeded&quot;</span>);
            <span class="hljs-comment">//CAS&#x8BBE;&#x7F6E;&#x8BFB;&#x9501;&#x6210;&#x529F;</span>
            <span class="hljs-keyword">if</span> (compareAndSetState(c, c + SHARED_UNIT)) {
                <span class="hljs-comment">//&#x5982;&#x679C;&#x662F;&#x7B2C;1&#x6B21;&#x83B7;&#x53D6;&#x201C;&#x8BFB;&#x53D6;&#x9501;&#x201D;&#xFF0C;&#x5219;&#x66F4;&#x65B0;firstReader&#x548C;firstReaderHoldCount</span>
                <span class="hljs-keyword">if</span> (sharedCount(c) == <span class="hljs-number">0</span>) {
                    firstReader = current;
                    firstReaderHoldCount = <span class="hljs-number">1</span>;
                }
                <span class="hljs-comment">//&#x5982;&#x679C;&#x60F3;&#x8981;&#x83B7;&#x53D6;&#x9501;&#x7684;&#x7EBF;&#x7A0B;(current)&#x662F;&#x7B2C;1&#x4E2A;&#x83B7;&#x53D6;&#x9501;(firstReader)&#x7684;&#x7EBF;&#x7A0B;,&#x5219;&#x5C06;firstReaderHoldCount+1</span>
                <span class="hljs-keyword">else</span> <span class="hljs-keyword">if</span> (firstReader == current) {
                    firstReaderHoldCount++;
                } <span class="hljs-keyword">else</span> {
                    <span class="hljs-keyword">if</span> (rh == <span class="hljs-keyword">null</span>)
                        rh = cachedHoldCounter;
                    <span class="hljs-keyword">if</span> (rh == <span class="hljs-keyword">null</span> || rh.tid != getThreadId(current))
                        rh = readHolds.get();
                    <span class="hljs-keyword">else</span> <span class="hljs-keyword">if</span> (rh.count == <span class="hljs-number">0</span>)
                        readHolds.set(rh);
                    <span class="hljs-comment">//&#x66F4;&#x65B0;&#x7EBF;&#x7A0B;&#x7684;&#x83B7;&#x53D6;&#x201C;&#x8BFB;&#x53D6;&#x9501;&#x201D;&#x7684;&#x5171;&#x4EAB;&#x8BA1;&#x6570;</span>
                    rh.count++;
                    cachedHoldCounter = rh; <span class="hljs-comment">// cache for release</span>
                }
                <span class="hljs-keyword">return</span> <span class="hljs-number">1</span>;
            }
        }
    }
</code></pre>
<p>fullTryAcquireShared(Thread current)&#x4F1A;&#x6839;&#x636E;&#x201C;&#x662F;&#x5426;&#x9700;&#x8981;&#x963B;&#x585E;&#x7B49;&#x5F85;&#x201D;&#xFF0C;&#x201C;&#x8BFB;&#x53D6;&#x9501;&#x7684;&#x5171;&#x4EAB;&#x8BA1;&#x6570;&#x662F;&#x5426;&#x8D85;&#x8FC7;&#x9650;&#x5236;&#x201D;&#x7B49;&#x7B49;&#x8FDB;&#x884C;&#x5904;&#x7406;&#x3002;&#x5982;&#x679C;&#x4E0D;&#x9700;&#x8981;&#x963B;&#x585E;&#x7B49;&#x5F85;&#xFF0C;&#x5E76;&#x4E14;&#x9501;&#x7684;&#x5171;&#x4EAB;&#x8BA1;&#x6570;&#x6CA1;&#x6709;&#x8D85;&#x8FC7;&#x9650;&#x5236;&#xFF0C;&#x5219;&#x901A;&#x8FC7;CAS&#x5C1D;&#x8BD5;&#x83B7;&#x53D6;&#x9501;&#xFF0C;&#x5E76;&#x8FD4;&#x56DE;1</p>
<h4 id="&#x8BFB;&#x9501;&#x7684;&#x91CA;&#x653E;"><a name="&#x8BFB;&#x9501;&#x7684;&#x91CA;&#x653E;" class="anchor-navigation-ex-anchor" href="#&#x8BFB;&#x9501;&#x7684;&#x91CA;&#x653E;"><i class="fa fa-link" aria-hidden="true"></i></a><a name="&#x8BFB;&#x9501;&#x7684;&#x91CA;&#x653E;" class="plugin-anchor" href="#&#x8BFB;&#x9501;&#x7684;&#x91CA;&#x653E;"><i class="fa fa-link" aria-hidden="true"></i></a>&#x8BFB;&#x9501;&#x7684;&#x91CA;&#x653E;</h4>
<p>&#x4E0E;&#x5199;&#x9501;&#x76F8;&#x540C;&#xFF0C;&#x8BFB;&#x9501;&#x4E5F;&#x63D0;&#x4F9B;&#x4E86;unlock()&#x91CA;&#x653E;&#x8BFB;&#x9501;&#xFF1A;</p>
<pre><code class="lang-java">        <span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-keyword">void</span> <span class="hljs-title">unlock</span><span class="hljs-params">()</span> </span>{
            sync.releaseShared(<span class="hljs-number">1</span>);
        }
</code></pre>
<p>unlcok()&#x65B9;&#x6CD5;&#x5185;&#x90E8;&#x4F7F;&#x7528;Sync&#x7684;releaseShared(int arg)&#x65B9;&#x6CD5;&#xFF0C;&#x8BE5;&#x65B9;&#x6CD5;&#x5B9A;&#x4E49;&#x5728;AQS&#x4E2D;&#xFF1A;</p>
<pre><code class="lang-java">    <span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-keyword">final</span> <span class="hljs-keyword">boolean</span> <span class="hljs-title">releaseShared</span><span class="hljs-params">(<span class="hljs-keyword">int</span> arg)</span> </span>{
        <span class="hljs-keyword">if</span> (tryReleaseShared(arg)) {
            doReleaseShared();
            <span class="hljs-keyword">return</span> <span class="hljs-keyword">true</span>;
        }
        <span class="hljs-keyword">return</span> <span class="hljs-keyword">false</span>;
    }
</code></pre>
<p>&#x8C03;&#x7528;tryReleaseShared(int arg)&#x5C1D;&#x8BD5;&#x91CA;&#x653E;&#x8BFB;&#x9501;&#xFF0C;&#x8BE5;&#x65B9;&#x6CD5;&#x5B9A;&#x4E49;&#x5728;&#x8BFB;&#x5199;&#x9501;&#x7684;Sync&#x5185;&#x90E8;&#x7C7B;&#x4E2D;&#xFF1A;</p>
<pre><code class="lang-java">    <span class="hljs-function"><span class="hljs-keyword">protected</span> <span class="hljs-keyword">final</span> <span class="hljs-keyword">boolean</span> <span class="hljs-title">tryReleaseShared</span><span class="hljs-params">(<span class="hljs-keyword">int</span> unused)</span> </span>{
        Thread current = Thread.currentThread();
        <span class="hljs-comment">//&#x5982;&#x679C;&#x60F3;&#x8981;&#x91CA;&#x653E;&#x9501;&#x7684;&#x7EBF;&#x7A0B;&#x4E3A;&#x7B2C;&#x4E00;&#x4E2A;&#x83B7;&#x53D6;&#x9501;&#x7684;&#x7EBF;&#x7A0B;</span>
        <span class="hljs-keyword">if</span> (firstReader == current) {
            <span class="hljs-comment">//&#x4EC5;&#x83B7;&#x53D6;&#x4E86;&#x4E00;&#x6B21;&#xFF0C;&#x5219;&#x9700;&#x8981;&#x5C06;firstReader &#x8BBE;&#x7F6E;null&#xFF0C;&#x5426;&#x5219; firstReaderHoldCount - 1</span>
            <span class="hljs-keyword">if</span> (firstReaderHoldCount == <span class="hljs-number">1</span>)
                firstReader = <span class="hljs-keyword">null</span>;
            <span class="hljs-keyword">else</span>
                firstReaderHoldCount--;
        }
        <span class="hljs-comment">//&#x83B7;&#x53D6;rh&#x5BF9;&#x8C61;&#xFF0C;&#x5E76;&#x66F4;&#x65B0;&#x201C;&#x5F53;&#x524D;&#x7EBF;&#x7A0B;&#x83B7;&#x53D6;&#x9501;&#x7684;&#x4FE1;&#x606F;&#x201D;</span>
        <span class="hljs-keyword">else</span> {
            HoldCounter rh = cachedHoldCounter;
            <span class="hljs-keyword">if</span> (rh == <span class="hljs-keyword">null</span> || rh.tid != getThreadId(current))
                rh = readHolds.get();
            <span class="hljs-keyword">int</span> count = rh.count;
            <span class="hljs-keyword">if</span> (count &lt;= <span class="hljs-number">1</span>) {
                readHolds.remove();
                <span class="hljs-keyword">if</span> (count &lt;= <span class="hljs-number">0</span>)
                    <span class="hljs-keyword">throw</span> unmatchedUnlockException();
            }
            --rh.count;
        }
        <span class="hljs-comment">//CAS&#x66F4;&#x65B0;&#x540C;&#x6B65;&#x72B6;&#x6001;</span>
        <span class="hljs-keyword">for</span> (;;) {
            <span class="hljs-keyword">int</span> c = getState();
            <span class="hljs-keyword">int</span> nextc = c - SHARED_UNIT;
            <span class="hljs-keyword">if</span> (compareAndSetState(c, nextc))
                <span class="hljs-keyword">return</span> nextc == <span class="hljs-number">0</span>;
        }
    }
</code></pre>
<h4 id="holdcounter"><a name="holdcounter" class="anchor-navigation-ex-anchor" href="#holdcounter"><i class="fa fa-link" aria-hidden="true"></i></a><a name="holdcounter" class="plugin-anchor" href="#holdcounter"><i class="fa fa-link" aria-hidden="true"></i></a>HoldCounter</h4>
<p>&#x5728;&#x8BFB;&#x9501;&#x83B7;&#x53D6;&#x9501;&#x548C;&#x91CA;&#x653E;&#x9501;&#x7684;&#x8FC7;&#x7A0B;&#x4E2D;&#xFF0C;&#x6211;&#x4EEC;&#x4E00;&#x76F4;&#x90FD;&#x53EF;&#x4EE5;&#x770B;&#x5230;&#x4E00;&#x4E2A;&#x53D8;&#x91CF;rh &#xFF08;HoldCounter &#xFF09;&#xFF0C;&#x8BE5;&#x53D8;&#x91CF;&#x5728;&#x8BFB;&#x9501;&#x4E2D;&#x626E;&#x6F14;&#x7740;&#x975E;&#x5E38;&#x91CD;&#x8981;&#x7684;&#x4F5C;&#x7528;&#x3002; &#x6211;&#x4EEC;&#x4E86;&#x89E3;&#x8BFB;&#x9501;&#x7684;&#x5185;&#x5728;&#x673A;&#x5236;&#x5176;&#x5B9E;&#x5C31;&#x662F;&#x4E00;&#x4E2A;&#x5171;&#x4EAB;&#x9501;&#xFF0C;&#x4E3A;&#x4E86;&#x66F4;&#x597D;&#x7406;&#x89E3;HoldCounter &#xFF0C;&#x6211;&#x4EEC;&#x6682;&#x4E14;&#x8BA4;&#x4E3A;&#x5B83;&#x4E0D;&#x662F;&#x4E00;&#x4E2A;&#x9501;&#x7684;&#x6982;&#x7387;&#xFF0C;&#x800C;&#x76F8;&#x5F53;&#x4E8E;&#x4E00;&#x4E2A;&#x8BA1;&#x6570;&#x5668;&#x3002;&#x4E00;&#x6B21;&#x5171;&#x4EAB;&#x9501;&#x7684;&#x64CD;&#x4F5C;&#x5C31;&#x76F8;&#x5F53;&#x4E8E;&#x5728;&#x8BE5;&#x8BA1;&#x6570;&#x5668;&#x7684;&#x64CD;&#x4F5C;&#x3002;&#x83B7;&#x53D6;&#x5171;&#x4EAB;&#x9501;&#xFF0C;&#x5219;&#x8BE5;&#x8BA1;&#x6570;&#x5668; + 1&#xFF0C;&#x91CA;&#x653E;&#x5171;&#x4EAB;&#x9501;&#xFF0C;&#x8BE5;&#x8BA1;&#x6570;&#x5668; - 1&#x3002;&#x53EA;&#x6709;&#x5F53;&#x7EBF;&#x7A0B;&#x83B7;&#x53D6;&#x5171;&#x4EAB;&#x9501;&#x540E;&#x624D;&#x80FD;&#x5BF9;&#x5171;&#x4EAB;&#x9501;&#x8FDB;&#x884C;&#x91CA;&#x653E;&#x3001;&#x91CD;&#x5165;&#x64CD;&#x4F5C;&#x3002;&#x6240;&#x4EE5;HoldCounter&#x7684;&#x4F5C;&#x7528;&#x5C31;&#x662F;&#x5F53;&#x524D;&#x7EBF;&#x7A0B;&#x6301;&#x6709;&#x5171;&#x4EAB;&#x9501;&#x7684;&#x6570;&#x91CF;&#xFF0C;&#x8FD9;&#x4E2A;&#x6570;&#x91CF;&#x5FC5;&#x987B;&#x8981;&#x4E0E;&#x7EBF;&#x7A0B;&#x7ED1;&#x5B9A;&#x5728;&#x4E00;&#x8D77;&#xFF0C;&#x5426;&#x5219;&#x64CD;&#x4F5C;&#x5176;&#x4ED6;&#x7EBF;&#x7A0B;&#x9501;&#x5C31;&#x4F1A;&#x629B;&#x51FA;&#x5F02;&#x5E38;&#x3002;&#x6211;&#x4EEC;&#x5148;&#x770B;HoldCounter&#x7684;&#x5B9A;&#x4E49;&#xFF1A;</p>
<pre><code class="lang-java">        <span class="hljs-keyword">static</span> <span class="hljs-keyword">final</span> <span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">HoldCounter</span> </span>{
            <span class="hljs-keyword">int</span> count = <span class="hljs-number">0</span>;
            <span class="hljs-keyword">final</span> <span class="hljs-keyword">long</span> tid = getThreadId(Thread.currentThread());
        }
</code></pre>
<p>HoldCounter &#x5B9A;&#x4E49;&#x975E;&#x5E38;&#x7B80;&#x5355;&#xFF0C;&#x5C31;&#x662F;&#x4E00;&#x4E2A;&#x8BA1;&#x6570;&#x5668;count &#x548C;&#x7EBF;&#x7A0B; id tid &#x4E24;&#x4E2A;&#x53D8;&#x91CF;&#x3002;&#x6309;&#x7167;&#x8FD9;&#x4E2A;&#x610F;&#x601D;&#x6211;&#x4EEC;&#x770B;&#x5230;HoldCounter &#x662F;&#x9700;&#x8981;&#x548C;&#x67D0;&#x7ED9;&#x7EBF;&#x7A0B;&#x8FDB;&#x884C;&#x7ED1;&#x5B9A;&#x4E86;&#xFF0C;&#x6211;&#x4EEC;&#x77E5;&#x9053;&#x5982;&#x679C;&#x8981;&#x5C06;&#x4E00;&#x4E2A;&#x5BF9;&#x8C61;&#x548C;&#x7EBF;&#x7A0B;&#x7ED1;&#x5B9A;&#x4EC5;&#x4EC5;&#x6709;tid&#x662F;&#x4E0D;&#x591F;&#x7684;&#xFF0C;&#x800C;&#x4E14;&#x4ECE;&#x4E0A;&#x9762;&#x7684;&#x4EE3;&#x7801;&#x6211;&#x4EEC;&#x53EF;&#x4EE5;&#x770B;&#x5230;HoldCounter &#x4EC5;&#x4EC5;&#x53EA;&#x662F;&#x8BB0;&#x5F55;&#x4E86;tid&#xFF0C;&#x6839;&#x672C;&#x8D77;&#x4E0D;&#x5230;&#x7ED1;&#x5B9A;&#x7EBF;&#x7A0B;&#x7684;&#x4F5C;&#x7528;&#x3002;&#x90A3;&#x4E48;&#x600E;&#x4E48;&#x5B9E;&#x73B0;&#x5462;&#xFF1F;&#x7B54;&#x6848;&#x662F;ThreadLocal&#xFF0C;&#x5B9A;&#x4E49;&#x5982;&#x4E0B;&#xFF1A;</p>
<pre><code class="lang-java">        <span class="hljs-keyword">static</span> <span class="hljs-keyword">final</span> <span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">ThreadLocalHoldCounter</span>
            <span class="hljs-keyword">extends</span> <span class="hljs-title">ThreadLocal</span>&lt;<span class="hljs-title">HoldCounter</span>&gt; </span>{
            <span class="hljs-function"><span class="hljs-keyword">public</span> HoldCounter <span class="hljs-title">initialValue</span><span class="hljs-params">()</span> </span>{
                <span class="hljs-keyword">return</span> <span class="hljs-keyword">new</span> HoldCounter();
            }
        }
</code></pre>
<p>&#x901A;&#x8FC7;&#x4E0A;&#x9762;&#x4EE3;&#x7801;HoldCounter&#x5C31;&#x53EF;&#x4EE5;&#x4E0E;&#x7EBF;&#x7A0B;&#x8FDB;&#x884C;&#x7ED1;&#x5B9A;&#x4E86;&#x3002;&#x6545;&#x800C;&#xFF0C;HoldCounter&#x5E94;&#x8BE5;&#x5C31;&#x662F;&#x7ED1;&#x5B9A;&#x7EBF;&#x7A0B;&#x4E0A;&#x7684;&#x4E00;&#x4E2A;&#x8BA1;&#x6570;&#x5668;&#xFF0C;&#x800C;ThradLocalHoldCounter&#x5219;&#x662F;&#x7EBF;&#x7A0B;&#x7ED1;&#x5B9A;&#x7684;ThreadLocal&#x3002;&#x4ECE;&#x4E0A;&#x9762;&#x6211;&#x4EEC;&#x53EF;&#x4EE5;&#x770B;&#x5230;ThreadLocal&#x5C06;HoldCounter&#x7ED1;&#x5B9A;&#x5230;&#x5F53;&#x524D;&#x7EBF;&#x7A0B;&#x4E0A;&#xFF0C;&#x540C;&#x65F6;HoldCounter&#x4E5F;&#x6301;&#x6709;&#x7EBF;&#x7A0B;Id&#xFF0C;&#x8FD9;&#x6837;&#x5728;&#x91CA;&#x653E;&#x9501;&#x7684;&#x65F6;&#x5019;&#x624D;&#x80FD;&#x77E5;&#x9053;ReadWriteLock&#x91CC;&#x9762;&#x7F13;&#x5B58;&#x7684;&#x4E0A;&#x4E00;&#x4E2A;&#x8BFB;&#x53D6;&#x7EBF;&#x7A0B;&#xFF08;cachedHoldCounter&#xFF09;&#x662F;&#x5426;&#x662F;&#x5F53;&#x524D;&#x7EBF;&#x7A0B;&#x3002;&#x8FD9;&#x6837;&#x505A;&#x7684;&#x597D;&#x5904;&#x662F;&#x53EF;&#x4EE5;&#x51CF;&#x5C11;ThreadLocal.get()&#x7684;&#x6B21;&#x6570;&#xFF0C;&#x56E0;&#x4E3A;&#x8FD9;&#x4E5F;&#x662F;&#x4E00;&#x4E2A;&#x8017;&#x65F6;&#x64CD;&#x4F5C;&#x3002;&#x9700;&#x8981;&#x8BF4;&#x660E;&#x7684;&#x662F;&#x8FD9;&#x6837;HoldCounter&#x7ED1;&#x5B9A;&#x7EBF;&#x7A0B;id&#x800C;&#x4E0D;&#x7ED1;&#x5B9A;&#x7EBF;&#x7A0B;&#x5BF9;&#x8C61;&#x7684;&#x539F;&#x56E0;&#x662F;&#x907F;&#x514D;HoldCounter&#x548C;ThreadLocal&#x4E92;&#x76F8;&#x7ED1;&#x5B9A;&#x800C;GC&#x96BE;&#x4EE5;&#x91CA;&#x653E;&#x5B83;&#x4EEC;&#xFF08;&#x5C3D;&#x7BA1;GC&#x80FD;&#x591F;&#x667A;&#x80FD;&#x7684;&#x53D1;&#x73B0;&#x8FD9;&#x79CD;&#x5F15;&#x7528;&#x800C;&#x56DE;&#x6536;&#x5B83;&#x4EEC;&#xFF0C;&#x4F46;&#x662F;&#x8FD9;&#x9700;&#x8981;&#x4E00;&#x5B9A;&#x7684;&#x4EE3;&#x4EF7;&#xFF09;&#xFF0C;&#x6240;&#x4EE5;&#x5176;&#x5B9E;&#x8FD9;&#x6837;&#x505A;&#x53EA;&#x662F;&#x4E3A;&#x4E86;&#x5E2E;&#x52A9;GC&#x5FEB;&#x901F;&#x56DE;&#x6536;&#x5BF9;&#x8C61;&#x800C;&#x5DF2;&#x3002; &#x770B;&#x5230;&#x8FD9;&#x91CC;&#x6211;&#x4EEC;&#x660E;&#x767D;&#x4E86;HoldCounter&#x4F5C;&#x7528;&#x4E86;&#xFF0C;&#x6211;&#x4EEC;&#x5728;&#x770B;&#x4E00;&#x4E2A;&#x83B7;&#x53D6;&#x8BFB;&#x9501;&#x7684;&#x4EE3;&#x7801;&#x6BB5;&#xFF1A;</p>
<pre><code class="lang-java">                <span class="hljs-keyword">else</span> <span class="hljs-keyword">if</span> (firstReader == current) {
                    firstReaderHoldCount++;
                } <span class="hljs-keyword">else</span> {
                    <span class="hljs-keyword">if</span> (rh == <span class="hljs-keyword">null</span>)
                        rh = cachedHoldCounter;
                    <span class="hljs-keyword">if</span> (rh == <span class="hljs-keyword">null</span> || rh.tid != getThreadId(current))
                        rh = readHolds.get();
                    <span class="hljs-keyword">else</span> <span class="hljs-keyword">if</span> (rh.count == <span class="hljs-number">0</span>)
                        readHolds.set(rh);
                    rh.count++;
                    cachedHoldCounter = rh; <span class="hljs-comment">// cache for release</span>
                }
</code></pre>
<p>&#x8FD9;&#x6BB5;&#x4EE3;&#x7801;&#x6D89;&#x53CA;&#x4E86;&#x51E0;&#x4E2A;&#x53D8;&#x91CF;&#xFF1A;firstReader &#x3001;firstReaderHoldCount&#x3001;cachedHoldCounter &#x3002;&#x6211;&#x4EEC;&#x5148;&#x7406;&#x6E05;&#x695A;&#x8FD9;&#x51E0;&#x4E2A;&#x53D8;&#x91CF;&#xFF1A;</p>
<pre><code class="lang-java"><span class="hljs-keyword">private</span> <span class="hljs-keyword">transient</span> Thread firstReader = <span class="hljs-keyword">null</span>;
<span class="hljs-keyword">private</span> <span class="hljs-keyword">transient</span> <span class="hljs-keyword">int</span> firstReaderHoldCount;
<span class="hljs-keyword">private</span> <span class="hljs-keyword">transient</span> HoldCounter cachedHoldCounter;
</code></pre>
<p>firstReader &#x770B;&#x540D;&#x5B57;&#x5C31;&#x660E;&#x767D;&#x4E86;&#x4E3A;&#x7B2C;&#x4E00;&#x4E2A;&#x83B7;&#x53D6;&#x8BFB;&#x9501;&#x7684;&#x7EBF;&#x7A0B;&#xFF0C;firstReaderHoldCount&#x4E3A;&#x7B2C;&#x4E00;&#x4E2A;&#x83B7;&#x53D6;&#x8BFB;&#x9501;&#x7684;&#x91CD;&#x5165;&#x6570;&#xFF0C;cachedHoldCounter&#x4E3A;HoldCounter&#x7684;&#x7F13;&#x5B58;&#x3002; &#x7406;&#x6E05;&#x695A;&#x4E0A;&#x9762;&#x6240;&#x6709;&#x7684;&#x53D8;&#x91CF;&#x4E86;&#xFF0C;HoldCounter&#x4E5F;&#x660E;&#x767D;&#x4E86;&#xFF0C;&#x6211;&#x4EEC;&#x5C31;&#x6765;&#x7ED9;&#x4E0A;&#x9762;&#x90A3;&#x6BB5;&#x4EE3;&#x7801;&#x6807;&#x660E;&#x6CE8;&#x91CA;&#xFF0C;&#x5982;&#x4E0B;&#xFF1A;</p>
<pre><code class="lang-java">    <span class="hljs-comment">//&#x5982;&#x679C;&#x83B7;&#x53D6;&#x8BFB;&#x9501;&#x7684;&#x7EBF;&#x7A0B;&#x4E3A;&#x7B2C;&#x4E00;&#x6B21;&#x83B7;&#x53D6;&#x8BFB;&#x9501;&#x7684;&#x7EBF;&#x7A0B;&#xFF0C;&#x5219;firstReaderHoldCount&#x91CD;&#x5165;&#x6570; + 1</span>
    <span class="hljs-keyword">else</span> <span class="hljs-keyword">if</span> (firstReader == current) {
        firstReaderHoldCount++;
    } <span class="hljs-keyword">else</span> {
        <span class="hljs-comment">//&#x975E;firstReader&#x8BA1;&#x6570;</span>
        <span class="hljs-keyword">if</span> (rh == <span class="hljs-keyword">null</span>)
            rh = cachedHoldCounter;
        <span class="hljs-comment">//rh == null &#x6216;&#x8005; rh.tid != current.getId()&#xFF0C;&#x9700;&#x8981;&#x83B7;&#x53D6;rh</span>
        <span class="hljs-keyword">if</span> (rh == <span class="hljs-keyword">null</span> || rh.tid != getThreadId(current))
            rh = readHolds.get();
            <span class="hljs-comment">//&#x52A0;&#x5165;&#x5230;readHolds&#x4E2D;</span>
        <span class="hljs-keyword">else</span> <span class="hljs-keyword">if</span> (rh.count == <span class="hljs-number">0</span>)
            readHolds.set(rh);
        <span class="hljs-comment">//&#x8BA1;&#x6570;+1</span>
        rh.count++;
        cachedHoldCounter = rh; <span class="hljs-comment">// cache for release</span>
    }
</code></pre>
<p>&#x8FD9;&#x91CC;&#x89E3;&#x91CA;&#x4E0B;&#x4E3A;&#x4F55;&#x8981;&#x5F15;&#x5165;firstRead&#x3001;firstReaderHoldCount&#x3002;&#x8FD9;&#x662F;&#x4E3A;&#x4E86;&#x4E00;&#x4E2A;&#x6548;&#x7387;&#x95EE;&#x9898;&#xFF0C;firstReader&#x662F;&#x4E0D;&#x4F1A;&#x653E;&#x5165;&#x5230;readHolds&#x4E2D;&#x7684;&#xFF0C;&#x5982;&#x679C;&#x8BFB;&#x9501;&#x4EC5;&#x6709;&#x4E00;&#x4E2A;&#x7684;&#x60C5;&#x51B5;&#x4E0B;&#x5C31;&#x4F1A;&#x907F;&#x514D;&#x67E5;&#x627E;readHolds&#x3002;</p>
<h3 id="&#x9501;&#x964D;&#x7EA7;"><a name="&#x9501;&#x964D;&#x7EA7;" class="anchor-navigation-ex-anchor" href="#&#x9501;&#x964D;&#x7EA7;"><i class="fa fa-link" aria-hidden="true"></i></a><a name="&#x9501;&#x964D;&#x7EA7;" class="plugin-anchor" href="#&#x9501;&#x964D;&#x7EA7;"><i class="fa fa-link" aria-hidden="true"></i></a>&#x9501;&#x964D;&#x7EA7;</h3>
<p>&#x4E0A;&#x5F00;&#x7BC7;&#x662F;LZ&#x5C31;&#x9610;&#x8FF0;&#x4E86;&#x8BFB;&#x5199;&#x9501;&#x6709;&#x4E00;&#x4E2A;&#x7279;&#x6027;&#x5C31;&#x662F;&#x9501;&#x964D;&#x7EA7;&#xFF0C;&#x9501;&#x964D;&#x7EA7;&#x5C31;&#x610F;&#x5473;&#x7740;&#x5199;&#x9501;&#x662F;&#x53EF;&#x4EE5;&#x964D;&#x7EA7;&#x4E3A;&#x8BFB;&#x9501;&#x7684;&#xFF0C;&#x4F46;&#x662F;&#x9700;&#x8981;&#x9075;&#x5FAA;&#x5148;&#x83B7;&#x53D6;&#x5199;&#x9501;&#x3001;&#x83B7;&#x53D6;&#x8BFB;&#x9501;&#x5728;&#x91CA;&#x653E;&#x5199;&#x9501;&#x7684;&#x6B21;&#x5E8F;&#x3002;&#x6CE8;&#x610F;&#x5982;&#x679C;&#x5F53;&#x524D;&#x7EBF;&#x7A0B;&#x5148;&#x83B7;&#x53D6;&#x5199;&#x9501;&#xFF0C;&#x7136;&#x540E;&#x91CA;&#x653E;&#x5199;&#x9501;&#xFF0C;&#x518D;&#x83B7;&#x53D6;&#x8BFB;&#x9501;&#x8FD9;&#x4E2A;&#x8FC7;&#x7A0B;&#x4E0D;&#x80FD;&#x79F0;&#x4E4B;&#x4E3A;&#x9501;&#x964D;&#x7EA7;&#xFF0C;&#x9501;&#x964D;&#x7EA7;&#x4E00;&#x5B9A;&#x8981;&#x9075;&#x5FAA;&#x90A3;&#x4E2A;&#x6B21;&#x5E8F;&#x3002; &#x5728;&#x83B7;&#x53D6;&#x8BFB;&#x9501;&#x7684;&#x65B9;&#x6CD5;tryAcquireShared(int unused)&#x4E2D;&#xFF0C;&#x6709;&#x4E00;&#x6BB5;&#x4EE3;&#x7801;&#x5C31;&#x662F;&#x6765;&#x5224;&#x8BFB;&#x9501;&#x964D;&#x7EA7;&#x7684;&#xFF1A;</p>
<pre><code class="lang-java">        <span class="hljs-keyword">int</span> c = getState();
        <span class="hljs-comment">//exclusiveCount(c)&#x8BA1;&#x7B97;&#x5199;&#x9501;</span>
        <span class="hljs-comment">//&#x5982;&#x679C;&#x5B58;&#x5728;&#x5199;&#x9501;&#xFF0C;&#x4E14;&#x9501;&#x7684;&#x6301;&#x6709;&#x8005;&#x4E0D;&#x662F;&#x5F53;&#x524D;&#x7EBF;&#x7A0B;&#xFF0C;&#x76F4;&#x63A5;&#x8FD4;&#x56DE;-1</span>
        <span class="hljs-comment">//&#x5B58;&#x5728;&#x9501;&#x964D;&#x7EA7;&#x95EE;&#x9898;&#xFF0C;&#x540E;&#x7EED;&#x9610;&#x8FF0;</span>
        <span class="hljs-keyword">if</span> (exclusiveCount(c) != <span class="hljs-number">0</span> &amp;&amp;
                getExclusiveOwnerThread() != current)
            <span class="hljs-keyword">return</span> -<span class="hljs-number">1</span>;
        <span class="hljs-comment">//&#x8BFB;&#x9501;</span>
        <span class="hljs-keyword">int</span> r = sharedCount(c);
</code></pre>
<p>&#x9501;&#x964D;&#x7EA7;&#x4E2D;&#x8BFB;&#x9501;&#x7684;&#x83B7;&#x53D6;&#x91CA;&#x653E;&#x4E3A;&#x5FC5;&#x8981;&#xFF1F;&#x80AF;&#x5B9A;&#x662F;&#x5FC5;&#x8981;&#x7684;&#x3002;&#x8BD5;&#x60F3;&#xFF0C;&#x5047;&#x5982;&#x5F53;&#x524D;&#x7EBF;&#x7A0B;A&#x4E0D;&#x83B7;&#x53D6;&#x8BFB;&#x9501;&#x800C;&#x662F;&#x76F4;&#x63A5;&#x91CA;&#x653E;&#x4E86;&#x5199;&#x9501;&#xFF0C;&#x8FD9;&#x4E2A;&#x65F6;&#x5019;&#x53E6;&#x5916;&#x4E00;&#x4E2A;&#x7EBF;&#x7A0B;B&#x83B7;&#x53D6;&#x4E86;&#x5199;&#x9501;&#xFF0C;&#x90A3;&#x4E48;&#x8FD9;&#x4E2A;&#x7EBF;&#x7A0B;B&#x5BF9;&#x6570;&#x636E;&#x7684;&#x4FEE;&#x6539;&#x662F;&#x4E0D;&#x4F1A;&#x5BF9;&#x5F53;&#x524D;&#x7EBF;&#x7A0B;A&#x53EF;&#x89C1;&#x7684;&#x3002;&#x5982;&#x679C;&#x83B7;&#x53D6;&#x4E86;&#x8BFB;&#x9501;&#xFF0C;&#x5219;&#x7EBF;&#x7A0B;B&#x5728;&#x83B7;&#x53D6;&#x5199;&#x9501;&#x8FC7;&#x7A0B;&#x4E2D;&#x5224;&#x65AD;&#x5982;&#x679C;&#x6709;&#x8BFB;&#x9501;&#x8FD8;&#x6CA1;&#x6709;&#x91CA;&#x653E;&#x5219;&#x4F1A;&#x88AB;&#x963B;&#x585E;&#xFF0C;&#x53EA;&#x6709;&#x5F53;&#x524D;&#x7EBF;&#x7A0B;A&#x91CA;&#x653E;&#x8BFB;&#x9501;&#x540E;&#xFF0C;&#x7EBF;&#x7A0B;B&#x624D;&#x4F1A;&#x83B7;&#x53D6;&#x5199;&#x9501;&#x6210;&#x529F;&#x3002;</p>
<h2 id="&#x56DB;-cyclicbarrier"><a name="&#x56DB;-cyclicbarrier" class="anchor-navigation-ex-anchor" href="#&#x56DB;-cyclicbarrier"><i class="fa fa-link" aria-hidden="true"></i></a><a name="&#x56DB;-cyclicbarrier" class="plugin-anchor" href="#&#x56DB;-cyclicbarrier"><i class="fa fa-link" aria-hidden="true"></i></a>&#x56DB; CyclicBarrier</h2>
<p>CyclicBarrier&#xFF0C;&#x4E00;&#x4E2A;&#x540C;&#x6B65;&#x8F85;&#x52A9;&#x7C7B;&#xFF0C;&#x5728;API&#x4E2D;&#x662F;&#x8FD9;&#x4E48;&#x4ECB;&#x7ECD;&#x7684;&#xFF1A; &#x5B83;&#x5141;&#x8BB8;&#x4E00;&#x7EC4;&#x7EBF;&#x7A0B;&#x4E92;&#x76F8;&#x7B49;&#x5F85;&#xFF0C;&#x76F4;&#x5230;&#x5230;&#x8FBE;&#x67D0;&#x4E2A;&#x516C;&#x5171;&#x5C4F;&#x969C;&#x70B9; (common barrier point)&#x3002;&#x5728;&#x6D89;&#x53CA;&#x4E00;&#x7EC4;&#x56FA;&#x5B9A;&#x5927;&#x5C0F;&#x7684;&#x7EBF;&#x7A0B;&#x7684;&#x7A0B;&#x5E8F;&#x4E2D;&#xFF0C;&#x8FD9;&#x4E9B;&#x7EBF;&#x7A0B;&#x5FC5;&#x987B;&#x4E0D;&#x65F6;&#x5730;&#x4E92;&#x76F8;&#x7B49;&#x5F85;&#xFF0C;&#x6B64;&#x65F6; CyclicBarrier &#x5F88;&#x6709;&#x7528;&#x3002;&#x56E0;&#x4E3A;&#x8BE5; barrier &#x5728;&#x91CA;&#x653E;&#x7B49;&#x5F85;&#x7EBF;&#x7A0B;&#x540E;&#x53EF;&#x4EE5;&#x91CD;&#x7528;&#xFF0C;&#x6240;&#x4EE5;&#x79F0;&#x5B83;&#x4E3A;&#x5FAA;&#x73AF; &#x7684; barrier&#x3002; &#x901A;&#x4FD7;&#x70B9;&#x8BB2;&#x5C31;&#x662F;&#xFF1A;&#x8BA9;&#x4E00;&#x7EC4;&#x7EBF;&#x7A0B;&#x5230;&#x8FBE;&#x4E00;&#x4E2A;&#x5C4F;&#x969C;&#x65F6;&#x88AB;&#x963B;&#x585E;&#xFF0C;&#x76F4;&#x5230;&#x6700;&#x540E;&#x4E00;&#x4E2A;&#x7EBF;&#x7A0B;&#x5230;&#x8FBE;&#x5C4F;&#x969C;&#x65F6;&#xFF0C;&#x5C4F;&#x969C;&#x624D;&#x4F1A;&#x5F00;&#x95E8;&#xFF0C;&#x6240;&#x6709;&#x88AB;&#x5C4F;&#x969C;&#x62E6;&#x622A;&#x7684;&#x7EBF;&#x7A0B;&#x624D;&#x4F1A;&#x7EE7;&#x7EED;&#x5E72;&#x6D3B;&#x3002;</p>
<h3 id="&#x5B9E;&#x73B0;&#x5206;&#x6790;"><a name="&#x5B9E;&#x73B0;&#x5206;&#x6790;" class="anchor-navigation-ex-anchor" href="#&#x5B9E;&#x73B0;&#x5206;&#x6790;"><i class="fa fa-link" aria-hidden="true"></i></a><a name="&#x5B9E;&#x73B0;&#x5206;&#x6790;" class="plugin-anchor" href="#&#x5B9E;&#x73B0;&#x5206;&#x6790;"><i class="fa fa-link" aria-hidden="true"></i></a>&#x5B9E;&#x73B0;&#x5206;&#x6790;</h3>
<p>CyclicBarrier&#x7684;&#x7ED3;&#x6784;&#x5982;&#x4E0B;&#xFF1A;</p>
<p><img src="https://gitee.com/zisuu/picture/raw/master/img/20201216213324.png" alt="image-20201216213323936"></p>
<p>&#x901A;&#x8FC7;&#x4E0A;&#x56FE;&#x6211;&#x4EEC;&#x53EF;&#x4EE5;&#x770B;&#x5230;CyclicBarrier&#x7684;&#x5185;&#x90E8;&#x662F;&#x4F7F;&#x7528;&#x91CD;&#x5165;&#x9501;ReentrantLock&#x548C;Condition&#x3002;&#x5B83;&#x6709;&#x4E24;&#x4E2A;&#x6784;&#x9020;&#x51FD;&#x6570;&#xFF1A;</p>
<ul>
<li>CyclicBarrier(int parties)&#xFF1A;&#x521B;&#x5EFA;&#x4E00;&#x4E2A;&#x65B0;&#x7684; CyclicBarrier&#xFF0C;&#x5B83;&#x5C06;&#x5728;&#x7ED9;&#x5B9A;&#x6570;&#x91CF;&#x7684;&#x53C2;&#x4E0E;&#x8005;&#xFF08;&#x7EBF;&#x7A0B;&#xFF09;&#x5904;&#x4E8E;&#x7B49;&#x5F85;&#x72B6;&#x6001;&#x65F6;&#x542F;&#x52A8;&#xFF0C;&#x4F46;&#x5B83;&#x4E0D;&#x4F1A;&#x5728;&#x542F;&#x52A8; barrier &#x65F6;&#x6267;&#x884C;&#x9884;&#x5B9A;&#x4E49;&#x7684;&#x64CD;&#x4F5C;&#x3002;</li>
<li>CyclicBarrier(int parties, Runnable barrierAction) &#xFF1A;&#x521B;&#x5EFA;&#x4E00;&#x4E2A;&#x65B0;&#x7684; CyclicBarrier&#xFF0C;&#x5B83;&#x5C06;&#x5728;&#x7ED9;&#x5B9A;&#x6570;&#x91CF;&#x7684;&#x53C2;&#x4E0E;&#x8005;&#xFF08;&#x7EBF;&#x7A0B;&#xFF09;&#x5904;&#x4E8E;&#x7B49;&#x5F85;&#x72B6;&#x6001;&#x65F6;&#x542F;&#x52A8;&#xFF0C;&#x5E76;&#x5728;&#x542F;&#x52A8; barrier &#x65F6;&#x6267;&#x884C;&#x7ED9;&#x5B9A;&#x7684;&#x5C4F;&#x969C;&#x64CD;&#x4F5C;&#xFF0C;&#x8BE5;&#x64CD;&#x4F5C;&#x7531;&#x6700;&#x540E;&#x4E00;&#x4E2A;&#x8FDB;&#x5165; barrier &#x7684;&#x7EBF;&#x7A0B;&#x6267;&#x884C;&#x3002;</li>
</ul>
<p>parties&#x8868;&#x793A;&#x62E6;&#x622A;&#x7EBF;&#x7A0B;&#x7684;&#x6570;&#x91CF;&#x3002; barrierAction &#x4E3A;CyclicBarrier&#x63A5;&#x6536;&#x7684;Runnable&#x547D;&#x4EE4;&#xFF0C;&#x7528;&#x4E8E;&#x5728;&#x7EBF;&#x7A0B;&#x5230;&#x8FBE;&#x5C4F;&#x969C;&#x65F6;&#xFF0C;&#x4F18;&#x5148;&#x6267;&#x884C;barrierAction &#xFF0C;&#x7528;&#x4E8E;&#x5904;&#x7406;&#x66F4;&#x52A0;&#x590D;&#x6742;&#x7684;&#x4E1A;&#x52A1;&#x573A;&#x666F;&#x3002;</p>
<pre><code class="lang-java">    <span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-title">CyclicBarrier</span><span class="hljs-params">(<span class="hljs-keyword">int</span> parties, Runnable barrierAction)</span> </span>{
        <span class="hljs-keyword">if</span> (parties &lt;= <span class="hljs-number">0</span>) <span class="hljs-keyword">throw</span> <span class="hljs-keyword">new</span> IllegalArgumentException();
        <span class="hljs-keyword">this</span>.parties = parties;
        <span class="hljs-keyword">this</span>.count = parties;
        <span class="hljs-keyword">this</span>.barrierCommand = barrierAction;
    }

    <span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-title">CyclicBarrier</span><span class="hljs-params">(<span class="hljs-keyword">int</span> parties)</span> </span>{
        <span class="hljs-keyword">this</span>(parties, <span class="hljs-keyword">null</span>);
    }
</code></pre>
<p>&#x5728;CyclicBarrier&#x4E2D;&#x6700;&#x91CD;&#x8981;&#x7684;&#x65B9;&#x6CD5;&#x83AB;&#x8FC7;&#x4E8E;await()&#x65B9;&#x6CD5;&#xFF0C;&#x5728;&#x6240;&#x6709;&#x53C2;&#x4E0E;&#x8005;&#x90FD;&#x5DF2;&#x7ECF;&#x5728;&#x6B64; barrier &#x4E0A;&#x8C03;&#x7528; await &#x65B9;&#x6CD5;&#x4E4B;&#x524D;&#xFF0C;&#x5C06;&#x4E00;&#x76F4;&#x7B49;&#x5F85;&#x3002;&#x5982;&#x4E0B;&#xFF1A;</p>
<pre><code class="lang-java">    <span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-keyword">int</span> <span class="hljs-title">await</span><span class="hljs-params">()</span> <span class="hljs-keyword">throws</span> InterruptedException, BrokenBarrierException </span>{
        <span class="hljs-keyword">try</span> {
            <span class="hljs-keyword">return</span> dowait(<span class="hljs-keyword">false</span>, <span class="hljs-number">0L</span>);<span class="hljs-comment">//&#x4E0D;&#x8D85;&#x65F6;&#x7B49;&#x5F85;</span>
        } <span class="hljs-keyword">catch</span> (TimeoutException toe) {
            <span class="hljs-keyword">throw</span> <span class="hljs-keyword">new</span> Error(toe); <span class="hljs-comment">// cannot happen</span>
        }
    }
</code></pre>
<p>await()&#x65B9;&#x6CD5;&#x5185;&#x90E8;&#x8C03;&#x7528;dowait(boolean timed, long nanos)&#x65B9;&#x6CD5;&#xFF1A;</p>
<pre><code class="lang-java">    <span class="hljs-function"><span class="hljs-keyword">private</span> <span class="hljs-keyword">int</span> <span class="hljs-title">dowait</span><span class="hljs-params">(<span class="hljs-keyword">boolean</span> timed, <span class="hljs-keyword">long</span> nanos)</span>
            <span class="hljs-keyword">throws</span> InterruptedException, BrokenBarrierException,
            TimeoutException </span>{
        <span class="hljs-comment">//&#x83B7;&#x53D6;&#x9501;</span>
        <span class="hljs-keyword">final</span> ReentrantLock lock = <span class="hljs-keyword">this</span>.lock;
        lock.lock();
        <span class="hljs-keyword">try</span> {
            <span class="hljs-comment">//&#x5206;&#x4EE3;</span>
            <span class="hljs-keyword">final</span> Generation g = generation;

            <span class="hljs-comment">//&#x5F53;&#x524D;generation&#x201C;&#x5DF2;&#x635F;&#x574F;&#x201D;&#xFF0C;&#x629B;&#x51FA;BrokenBarrierException&#x5F02;&#x5E38;</span>
            <span class="hljs-comment">//&#x629B;&#x51FA;&#x8BE5;&#x5F02;&#x5E38;&#x4E00;&#x822C;&#x90FD;&#x662F;&#x67D0;&#x4E2A;&#x7EBF;&#x7A0B;&#x5728;&#x7B49;&#x5F85;&#x67D0;&#x4E2A;&#x5904;&#x4E8E;&#x201C;&#x65AD;&#x5F00;&#x201D;&#x72B6;&#x6001;&#x7684;CyclicBarrie</span>
            <span class="hljs-keyword">if</span> (g.broken)
                <span class="hljs-comment">//&#x5F53;&#x67D0;&#x4E2A;&#x7EBF;&#x7A0B;&#x8BD5;&#x56FE;&#x7B49;&#x5F85;&#x5904;&#x4E8E;&#x65AD;&#x5F00;&#x72B6;&#x6001;&#x7684; barrier &#x65F6;&#xFF0C;&#x6216;&#x8005; barrier &#x8FDB;&#x5165;&#x65AD;&#x5F00;&#x72B6;&#x6001;&#x800C;&#x7EBF;&#x7A0B;&#x5904;&#x4E8E;&#x7B49;&#x5F85;&#x72B6;&#x6001;&#x65F6;&#xFF0C;&#x629B;&#x51FA;&#x8BE5;&#x5F02;&#x5E38;</span>
                <span class="hljs-keyword">throw</span> <span class="hljs-keyword">new</span> BrokenBarrierException();

            <span class="hljs-comment">//&#x5982;&#x679C;&#x7EBF;&#x7A0B;&#x4E2D;&#x65AD;&#xFF0C;&#x7EC8;&#x6B62;CyclicBarrier</span>
            <span class="hljs-keyword">if</span> (Thread.interrupted()) {
                breakBarrier();
                <span class="hljs-keyword">throw</span> <span class="hljs-keyword">new</span> InterruptedException();
            }

            <span class="hljs-comment">//&#x8FDB;&#x6765;&#x4E00;&#x4E2A;&#x7EBF;&#x7A0B; count - 1</span>
            <span class="hljs-keyword">int</span> index = --count;
            <span class="hljs-comment">//count == 0 &#x8868;&#x793A;&#x6240;&#x6709;&#x7EBF;&#x7A0B;&#x5747;&#x5DF2;&#x5230;&#x4F4D;&#xFF0C;&#x89E6;&#x53D1;Runnable&#x4EFB;&#x52A1;</span>
            <span class="hljs-keyword">if</span> (index == <span class="hljs-number">0</span>) {  <span class="hljs-comment">// tripped</span>
                <span class="hljs-keyword">boolean</span> ranAction = <span class="hljs-keyword">false</span>;
                <span class="hljs-keyword">try</span> {
                    <span class="hljs-keyword">final</span> Runnable command = barrierCommand;
                    <span class="hljs-comment">//&#x89E6;&#x53D1;&#x4EFB;&#x52A1;</span>
                    <span class="hljs-keyword">if</span> (command != <span class="hljs-keyword">null</span>)
                        command.run();
                    ranAction = <span class="hljs-keyword">true</span>;
                    <span class="hljs-comment">//&#x5524;&#x9192;&#x6240;&#x6709;&#x7B49;&#x5F85;&#x7EBF;&#x7A0B;&#xFF0C;&#x5E76;&#x66F4;&#x65B0;generation</span>
                    nextGeneration();
                    <span class="hljs-keyword">return</span> <span class="hljs-number">0</span>;
                } <span class="hljs-keyword">finally</span> {
                    <span class="hljs-keyword">if</span> (!ranAction)
                        breakBarrier();
                }
            }


            <span class="hljs-keyword">for</span> (;;) {
                <span class="hljs-keyword">try</span> {
                    <span class="hljs-comment">//&#x5982;&#x679C;&#x4E0D;&#x662F;&#x8D85;&#x65F6;&#x7B49;&#x5F85;&#xFF0C;&#x5219;&#x8C03;&#x7528;Condition.await()&#x65B9;&#x6CD5;&#x7B49;&#x5F85;</span>
                    <span class="hljs-keyword">if</span> (!timed)
                        trip.await();
                    <span class="hljs-keyword">else</span> <span class="hljs-keyword">if</span> (nanos &gt; <span class="hljs-number">0L</span>)
                        <span class="hljs-comment">//&#x8D85;&#x65F6;&#x7B49;&#x5F85;&#xFF0C;&#x8C03;&#x7528;Condition.awaitNanos()&#x65B9;&#x6CD5;&#x7B49;&#x5F85;</span>
                        nanos = trip.awaitNanos(nanos);
                } <span class="hljs-keyword">catch</span> (InterruptedException ie) {
                    <span class="hljs-keyword">if</span> (g == generation &amp;&amp; ! g.broken) {
                        breakBarrier();
                        <span class="hljs-keyword">throw</span> ie;
                    } <span class="hljs-keyword">else</span> {
                        <span class="hljs-comment">// We&apos;re about to finish waiting even if we had not</span>
                        <span class="hljs-comment">// been interrupted, so this interrupt is deemed to</span>
                        <span class="hljs-comment">// &quot;belong&quot; to subsequent execution.</span>
                        Thread.currentThread().interrupt();
                    }
                }

                <span class="hljs-keyword">if</span> (g.broken)
                    <span class="hljs-keyword">throw</span> <span class="hljs-keyword">new</span> BrokenBarrierException();

                <span class="hljs-comment">//generation&#x5DF2;&#x7ECF;&#x66F4;&#x65B0;&#xFF0C;&#x8FD4;&#x56DE;index</span>
                <span class="hljs-keyword">if</span> (g != generation)
                    <span class="hljs-keyword">return</span> index;

                <span class="hljs-comment">//&#x201C;&#x8D85;&#x65F6;&#x7B49;&#x5F85;&#x201D;&#xFF0C;&#x5E76;&#x4E14;&#x65F6;&#x95F4;&#x5DF2;&#x5230;,&#x7EC8;&#x6B62;CyclicBarrier&#xFF0C;&#x5E76;&#x629B;&#x51FA;&#x5F02;&#x5E38;</span>
                <span class="hljs-keyword">if</span> (timed &amp;&amp; nanos &lt;= <span class="hljs-number">0L</span>) {
                    breakBarrier();
                    <span class="hljs-keyword">throw</span> <span class="hljs-keyword">new</span> TimeoutException();
                }
            }
        } <span class="hljs-keyword">finally</span> {
            <span class="hljs-comment">//&#x91CA;&#x653E;&#x9501;</span>
            lock.unlock();
        }
    }
</code></pre>
<p>&#x5176;&#x5B9E;await()&#x7684;&#x5904;&#x7406;&#x903B;&#x8F91;&#x8FD8;&#x662F;&#x6BD4;&#x8F83;&#x7B80;&#x5355;&#x7684;&#xFF1A;&#x5982;&#x679C;&#x8BE5;&#x7EBF;&#x7A0B;&#x4E0D;&#x662F;&#x5230;&#x8FBE;&#x7684;&#x6700;&#x540E;&#x4E00;&#x4E2A;&#x7EBF;&#x7A0B;&#xFF0C;&#x5219;&#x4ED6;&#x4F1A;&#x4E00;&#x76F4;&#x5904;&#x4E8E;&#x7B49;&#x5F85;&#x72B6;&#x6001;&#xFF0C;&#x9664;&#x975E;&#x53D1;&#x751F;&#x4EE5;&#x4E0B;&#x60C5;&#x51B5;&#xFF1A;</p>
<ol>
<li>&#x6700;&#x540E;&#x4E00;&#x4E2A;&#x7EBF;&#x7A0B;&#x5230;&#x8FBE;&#xFF0C;&#x5373;index == 0</li>
<li>&#x8D85;&#x51FA;&#x4E86;&#x6307;&#x5B9A;&#x65F6;&#x95F4;&#xFF08;&#x8D85;&#x65F6;&#x7B49;&#x5F85;&#xFF09;</li>
<li>&#x5176;&#x4ED6;&#x7684;&#x67D0;&#x4E2A;&#x7EBF;&#x7A0B;&#x4E2D;&#x65AD;&#x5F53;&#x524D;&#x7EBF;&#x7A0B;</li>
<li>&#x5176;&#x4ED6;&#x7684;&#x67D0;&#x4E2A;&#x7EBF;&#x7A0B;&#x4E2D;&#x65AD;&#x53E6;&#x4E00;&#x4E2A;&#x7B49;&#x5F85;&#x7684;&#x7EBF;&#x7A0B;</li>
<li>&#x5176;&#x4ED6;&#x7684;&#x67D0;&#x4E2A;&#x7EBF;&#x7A0B;&#x5728;&#x7B49;&#x5F85;barrier&#x8D85;&#x65F6;</li>
<li>&#x5176;&#x4ED6;&#x7684;&#x67D0;&#x4E2A;&#x7EBF;&#x7A0B;&#x5728;&#x6B64;barrier&#x8C03;&#x7528;reset()&#x65B9;&#x6CD5;&#x3002;reset()&#x65B9;&#x6CD5;&#x7528;&#x4E8E;&#x5C06;&#x5C4F;&#x969C;&#x91CD;&#x7F6E;&#x4E3A;&#x521D;&#x59CB;&#x72B6;&#x6001;&#x3002;</li>
</ol>
<p>&#x5728;&#x4E0A;&#x9762;&#x7684;&#x6E90;&#x4EE3;&#x7801;&#x4E2D;&#xFF0C;&#x6211;&#x4EEC;&#x53EF;&#x80FD;&#x9700;&#x8981;&#x6CE8;&#x610F;Generation &#x5BF9;&#x8C61;&#xFF0C;&#x5728;&#x4E0A;&#x8FF0;&#x4EE3;&#x7801;&#x4E2D;&#x6211;&#x4EEC;&#x603B;&#x662F;&#x53EF;&#x4EE5;&#x770B;&#x5230;&#x629B;&#x51FA;BrokenBarrierException&#x5F02;&#x5E38;&#xFF0C;&#x90A3;&#x4E48;&#x4EC0;&#x4E48;&#x65F6;&#x5019;&#x629B;&#x51FA;&#x5F02;&#x5E38;&#x5462;&#xFF1F;&#x5982;&#x679C;&#x4E00;&#x4E2A;&#x7EBF;&#x7A0B;&#x5904;&#x4E8E;&#x7B49;&#x5F85;&#x72B6;&#x6001;&#x65F6;&#xFF0C;&#x5982;&#x679C;&#x5176;&#x4ED6;&#x7EBF;&#x7A0B;&#x8C03;&#x7528;reset()&#xFF0C;&#x6216;&#x8005;&#x8C03;&#x7528;&#x7684;barrier&#x539F;&#x672C;&#x5C31;&#x662F;&#x88AB;&#x635F;&#x574F;&#x7684;&#xFF0C;&#x5219;&#x629B;&#x51FA;BrokenBarrierException&#x5F02;&#x5E38;&#x3002;&#x540C;&#x65F6;&#xFF0C;&#x4EFB;&#x4F55;&#x7EBF;&#x7A0B;&#x5728;&#x7B49;&#x5F85;&#x65F6;&#x88AB;&#x4E2D;&#x65AD;&#x4E86;&#xFF0C;&#x5219;&#x5176;&#x4ED6;&#x6240;&#x6709;&#x7EBF;&#x7A0B;&#x90FD;&#x5C06;&#x629B;&#x51FA;BrokenBarrierException&#x5F02;&#x5E38;&#xFF0C;&#x5E76;&#x5C06;barrier&#x7F6E;&#x4E8E;&#x635F;&#x574F;&#x72B6;&#x6001;&#x3002; &#x540C;&#x65F6;&#xFF0C;Generation&#x63CF;&#x8FF0;&#x7740;CyclicBarrier&#x7684;&#x66F4;&#x663E;&#x6362;&#x4EE3;&#x3002;&#x5728;CyclicBarrier&#x4E2D;&#xFF0C;&#x540C;&#x4E00;&#x6279;&#x7EBF;&#x7A0B;&#x5C5E;&#x4E8E;&#x540C;&#x4E00;&#x4EE3;&#x3002;&#x5F53;&#x6709;parties&#x4E2A;&#x7EBF;&#x7A0B;&#x5230;&#x8FBE;barrier&#xFF0C;generation&#x5C31;&#x4F1A;&#x88AB;&#x66F4;&#x65B0;&#x6362;&#x4EE3;&#x3002;&#x5176;&#x4E2D;broken&#x6807;&#x8BC6;&#x8BE5;&#x5F53;&#x524D;CyclicBarrier&#x662F;&#x5426;&#x5DF2;&#x7ECF;&#x5904;&#x4E8E;&#x4E2D;&#x65AD;&#x72B6;&#x6001;&#x3002;</p>
<pre><code class="lang-java">    <span class="hljs-keyword">private</span> <span class="hljs-keyword">static</span> <span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">Generation</span> </span>{
        <span class="hljs-keyword">boolean</span> broken = <span class="hljs-keyword">false</span>;
    }
</code></pre>
<p>&#x9ED8;&#x8BA4;barrier&#x662F;&#x6CA1;&#x6709;&#x635F;&#x574F;&#x7684;&#x3002; &#x5F53;barrier&#x635F;&#x574F;&#x4E86;&#x6216;&#x8005;&#x6709;&#x4E00;&#x4E2A;&#x7EBF;&#x7A0B;&#x4E2D;&#x65AD;&#x4E86;&#xFF0C;&#x5219;&#x901A;&#x8FC7;breakBarrier()&#x6765;&#x7EC8;&#x6B62;&#x6240;&#x6709;&#x7684;&#x7EBF;&#x7A0B;&#xFF1A;</p>
<pre><code class="lang-java">    <span class="hljs-function"><span class="hljs-keyword">private</span> <span class="hljs-keyword">void</span> <span class="hljs-title">breakBarrier</span><span class="hljs-params">()</span> </span>{
        generation.broken = <span class="hljs-keyword">true</span>;
        count = parties;
        trip.signalAll();
    }
</code></pre>
<p>&#x5728;breakBarrier()&#x4E2D;&#x9664;&#x4E86;&#x5C06;broken&#x8BBE;&#x7F6E;&#x4E3A;true&#xFF0C;&#x8FD8;&#x4F1A;&#x8C03;&#x7528;signalAll&#x5C06;&#x5728;CyclicBarrier&#x5904;&#x4E8E;&#x7B49;&#x5F85;&#x72B6;&#x6001;&#x7684;&#x7EBF;&#x7A0B;&#x5168;&#x90E8;&#x5524;&#x9192;&#x3002; &#x5F53;&#x6240;&#x6709;&#x7EBF;&#x7A0B;&#x90FD;&#x5DF2;&#x7ECF;&#x5230;&#x8FBE;barrier&#x5904;&#xFF08;index == 0&#xFF09;&#xFF0C;&#x5219;&#x4F1A;&#x901A;&#x8FC7;nextGeneration()&#x8FDB;&#x884C;&#x66F4;&#x65B0;&#x6362;&#x5730;&#x64CD;&#x4F5C;&#xFF0C;&#x5728;&#x8FD9;&#x4E2A;&#x6B65;&#x9AA4;&#x4E2D;&#xFF0C;&#x505A;&#x4E86;&#x4E09;&#x4EF6;&#x4E8B;&#xFF1A;&#x5524;&#x9192;&#x6240;&#x6709;&#x7EBF;&#x7A0B;&#xFF0C;&#x91CD;&#x7F6E;count&#xFF0C;generation&#x3002;</p>
<pre><code class="lang-java">    <span class="hljs-function"><span class="hljs-keyword">private</span> <span class="hljs-keyword">void</span> <span class="hljs-title">nextGeneration</span><span class="hljs-params">()</span> </span>{
        trip.signalAll();
        count = parties;
        generation = <span class="hljs-keyword">new</span> Generation();
    }
</code></pre>
<p>CyclicBarrier&#x540C;&#x65F6;&#x4E5F;&#x63D0;&#x4F9B;&#x4E86;await(long timeout, TimeUnit unit) &#x65B9;&#x6CD5;&#x6765;&#x505A;&#x8D85;&#x65F6;&#x63A7;&#x5236;&#xFF0C;&#x5185;&#x90E8;&#x8FD8;&#x662F;&#x901A;&#x8FC7;&#x8C03;&#x7528;doawait()&#x5B9E;&#x73B0;&#x7684;&#x3002;</p>
<h3 id="&#x5E94;&#x7528;&#x573A;&#x666F;"><a name="&#x5E94;&#x7528;&#x573A;&#x666F;" class="anchor-navigation-ex-anchor" href="#&#x5E94;&#x7528;&#x573A;&#x666F;"><i class="fa fa-link" aria-hidden="true"></i></a><a name="&#x5E94;&#x7528;&#x573A;&#x666F;" class="plugin-anchor" href="#&#x5E94;&#x7528;&#x573A;&#x666F;"><i class="fa fa-link" aria-hidden="true"></i></a>&#x5E94;&#x7528;&#x573A;&#x666F;</h3>
<p>CyclicBarrier&#x8BD5;&#x7528;&#x4E0E;&#x591A;&#x7EBF;&#x7A0B;&#x7ED3;&#x679C;&#x5408;&#x5E76;&#x7684;&#x64CD;&#x4F5C;&#xFF0C;&#x7528;&#x4E8E;&#x591A;&#x7EBF;&#x7A0B;&#x8BA1;&#x7B97;&#x6570;&#x636E;&#xFF0C;&#x6700;&#x540E;&#x5408;&#x5E76;&#x8BA1;&#x7B97;&#x7ED3;&#x679C;&#x7684;&#x5E94;&#x7528;&#x573A;&#x666F;&#x3002;&#x6BD4;&#x5982;&#x6211;&#x4EEC;&#x9700;&#x8981;&#x7EDF;&#x8BA1;&#x591A;&#x4E2A;Excel&#x4E2D;&#x7684;&#x6570;&#x636E;&#xFF0C;&#x7136;&#x540E;&#x7B49;&#x5230;&#x4E00;&#x4E2A;&#x603B;&#x7ED3;&#x679C;&#x3002;&#x6211;&#x4EEC;&#x53EF;&#x4EE5;&#x901A;&#x8FC7;&#x591A;&#x7EBF;&#x7A0B;&#x5904;&#x7406;&#x6BCF;&#x4E00;&#x4E2A;Excel&#xFF0C;&#x6267;&#x884C;&#x5B8C;&#x6210;&#x540E;&#x5F97;&#x5230;&#x76F8;&#x5E94;&#x7684;&#x7ED3;&#x679C;&#xFF0C;&#x6700;&#x540E;&#x901A;&#x8FC7;barrierAction&#x6765;&#x8BA1;&#x7B97;&#x8FD9;&#x4E9B;&#x7EBF;&#x7A0B;&#x7684;&#x8BA1;&#x7B97;&#x7ED3;&#x679C;&#xFF0C;&#x5F97;&#x5230;&#x6240;&#x6709;Excel&#x7684;&#x603B;&#x548C;&#x3002;</p>
<h3 id="&#x5E94;&#x7528;&#x793A;&#x4F8B;"><a name="&#x5E94;&#x7528;&#x793A;&#x4F8B;" class="anchor-navigation-ex-anchor" href="#&#x5E94;&#x7528;&#x793A;&#x4F8B;"><i class="fa fa-link" aria-hidden="true"></i></a><a name="&#x5E94;&#x7528;&#x793A;&#x4F8B;" class="plugin-anchor" href="#&#x5E94;&#x7528;&#x793A;&#x4F8B;"><i class="fa fa-link" aria-hidden="true"></i></a>&#x5E94;&#x7528;&#x793A;&#x4F8B;</h3>
<p>&#x6BD4;&#x5982;&#x6211;&#x4EEC;&#x5F00;&#x4F1A;&#x53EA;&#x6709;&#x7B49;&#x6240;&#x6709;&#x7684;&#x4EBA;&#x5230;&#x9F50;&#x4E86;&#x624D;&#x4F1A;&#x5F00;&#x4F1A;&#xFF0C;&#x5982;&#x4E0B;&#xFF1A;</p>
<pre><code class="lang-java"><span class="hljs-keyword">public</span> <span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">CyclicBarrierTest</span> </span>{
    <span class="hljs-keyword">private</span> <span class="hljs-keyword">static</span> CyclicBarrier cyclicBarrier;

    <span class="hljs-keyword">static</span> <span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">CyclicBarrierThread</span> <span class="hljs-keyword">extends</span> <span class="hljs-title">Thread</span></span>{
        <span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-keyword">void</span> <span class="hljs-title">run</span><span class="hljs-params">()</span> </span>{
            System.out.println(Thread.currentThread().getName() + <span class="hljs-string">&quot;&#x5230;&#x4E86;&quot;</span>);
            <span class="hljs-comment">//&#x7B49;&#x5F85;</span>
            <span class="hljs-keyword">try</span> {
                cyclicBarrier.await();
            } <span class="hljs-keyword">catch</span> (Exception e) {
                e.printStackTrace();
            }
        }
    }

    <span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-keyword">static</span> <span class="hljs-keyword">void</span> <span class="hljs-title">main</span><span class="hljs-params">(String[] args)</span></span>{
        cyclicBarrier = <span class="hljs-keyword">new</span> CyclicBarrier(<span class="hljs-number">5</span>, <span class="hljs-keyword">new</span> Runnable() {
            <span class="hljs-meta">@Override</span>
            <span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-keyword">void</span> <span class="hljs-title">run</span><span class="hljs-params">()</span> </span>{
                System.out.println(<span class="hljs-string">&quot;&#x4EBA;&#x5230;&#x9F50;&#x4E86;&#xFF0C;&#x5F00;&#x4F1A;&#x5427;....&quot;</span>);
            }
        });

        <span class="hljs-keyword">for</span>(<span class="hljs-keyword">int</span> i = <span class="hljs-number">0</span> ; i &lt; <span class="hljs-number">5</span> ; i++){
            <span class="hljs-keyword">new</span> CyclicBarrierThread().start();
        }
    }
}
</code></pre>
<p><img src="https://gitee.com/zisuu/picture/raw/master/img/20201216213334.png" alt="image-20201216213334432"></p>
<h2 id="&#x4E94;-countdownlatch"><a name="&#x4E94;-countdownlatch" class="anchor-navigation-ex-anchor" href="#&#x4E94;-countdownlatch"><i class="fa fa-link" aria-hidden="true"></i></a><a name="&#x4E94;-countdownlatch" class="plugin-anchor" href="#&#x4E94;-countdownlatch"><i class="fa fa-link" aria-hidden="true"></i></a>&#x4E94; countDownLatch</h2>
<p>CyclicBarrier&#x6240;&#x63CF;&#x8FF0;&#x7684;&#x662F;&#x201C;&#x5141;&#x8BB8;&#x4E00;&#x7EC4;&#x7EBF;&#x7A0B;&#x4E92;&#x76F8;&#x7B49;&#x5F85;&#xFF0C;&#x76F4;&#x5230;&#x5230;&#x8FBE;&#x67D0;&#x4E2A;&#x516C;&#x5171;&#x5C4F;&#x969C;&#x70B9;&#xFF0C;&#x624D;&#x4F1A;&#x8FDB;&#x884C;&#x540E;&#x7EED;&#x4EFB;&#x52A1;&quot;&#xFF0C;&#x800C;CountDownLatch&#x6240;&#x63CF;&#x8FF0;&#x7684;&#x662F;&#x201D;&#x5728;&#x5B8C;&#x6210;&#x4E00;&#x7EC4;&#x6B63;&#x5728;&#x5176;&#x4ED6;&#x7EBF;&#x7A0B;&#x4E2D;&#x6267;&#x884C;&#x7684;&#x64CD;&#x4F5C;&#x4E4B;&#x524D;&#xFF0C;&#x5B83;&#x5141;&#x8BB8;&#x4E00;&#x4E2A;&#x6216;&#x591A;&#x4E2A;&#x7EBF;&#x7A0B;&#x4E00;&#x76F4;&#x7B49;&#x5F85;&#x201C;&#x3002;&#x5728;API&#x4E2D;&#x662F;&#x8FD9;&#x6837;&#x63CF;&#x8FF0;&#x7684;&#xFF1A;</p>
<blockquote>
<p>&#x7528;&#x7ED9;&#x5B9A;&#x7684;&#x8BA1;&#x6570; &#x521D;&#x59CB;&#x5316; CountDownLatch&#x3002;&#x7531;&#x4E8E;&#x8C03;&#x7528;&#x4E86; countDown() &#x65B9;&#x6CD5;&#xFF0C;&#x6240;&#x4EE5;&#x5728;&#x5F53;&#x524D;&#x8BA1;&#x6570;&#x5230;&#x8FBE;&#x96F6;&#x4E4B;&#x524D;&#xFF0C;await &#x65B9;&#x6CD5;&#x4F1A;&#x4E00;&#x76F4;&#x53D7;&#x963B;&#x585E;&#x3002;&#x4E4B;&#x540E;&#xFF0C;&#x4F1A;&#x91CA;&#x653E;&#x6240;&#x6709;&#x7B49;&#x5F85;&#x7684;&#x7EBF;&#x7A0B;&#xFF0C;await &#x7684;&#x6240;&#x6709;&#x540E;&#x7EED;&#x8C03;&#x7528;&#x90FD;&#x5C06;&#x7ACB;&#x5373;&#x8FD4;&#x56DE;&#x3002;&#x8FD9;&#x79CD;&#x73B0;&#x8C61;&#x53EA;&#x51FA;&#x73B0;&#x4E00;&#x6B21;&#x2014;&#x2014;&#x8BA1;&#x6570;&#x65E0;&#x6CD5;&#x88AB;&#x91CD;&#x7F6E;&#x3002;&#x5982;&#x679C;&#x9700;&#x8981;&#x91CD;&#x7F6E;&#x8BA1;&#x6570;&#xFF0C;&#x8BF7;&#x8003;&#x8651;&#x4F7F;&#x7528; CyclicBarrier&#x3002;</p>
</blockquote>
<p><img src="https://gitee.com/zisuu/picture/raw/master/img/20201216213340.png" alt="image-20201216213340754"></p>
<p>CountDownLatch&#x662F;&#x901A;&#x8FC7;&#x4E00;&#x4E2A;&#x8BA1;&#x6570;&#x5668;&#x6765;&#x5B9E;&#x73B0;&#x7684;&#xFF0C;&#x5F53;&#x6211;&#x4EEC;&#x5728;new &#x4E00;&#x4E2A;CountDownLatch&#x5BF9;&#x8C61;&#x7684;&#x65F6;&#x5019;&#x9700;&#x8981;&#x5E26;&#x5165;&#x8BE5;&#x8BA1;&#x6570;&#x5668;&#x503C;&#xFF0C;&#x8BE5;&#x503C;&#x5C31;&#x8868;&#x793A;&#x4E86;&#x7EBF;&#x7A0B;&#x7684;&#x6570;&#x91CF;&#x3002;&#x6BCF;&#x5F53;&#x4E00;&#x4E2A;&#x7EBF;&#x7A0B;&#x5B8C;&#x6210;&#x81EA;&#x5DF1;&#x7684;&#x4EFB;&#x52A1;&#x540E;&#xFF0C;&#x8BA1;&#x6570;&#x5668;&#x7684;&#x503C;&#x5C31;&#x4F1A;&#x51CF;1&#x3002;&#x5F53;&#x8BA1;&#x6570;&#x5668;&#x7684;&#x503C;&#x53D8;&#x4E3A;0&#x65F6;&#xFF0C;&#x5C31;&#x8868;&#x793A;&#x6240;&#x6709;&#x7684;&#x7EBF;&#x7A0B;&#x5747;&#x5DF2;&#x7ECF;&#x5B8C;&#x6210;&#x4E86;&#x4EFB;&#x52A1;&#xFF0C;&#x7136;&#x540E;&#x5C31;&#x53EF;&#x4EE5;&#x6062;&#x590D;&#x7B49;&#x5F85;&#x7684;&#x7EBF;&#x7A0B;&#x7EE7;&#x7EED;&#x6267;&#x884C;&#x4E86;&#x3002;</p>
<p>&#x867D;&#x7136;&#xFF0C;CountDownlatch&#x4E0E;CyclicBarrier&#x6709;&#x90A3;&#x4E48;&#x70B9;&#x76F8;&#x4F3C;&#xFF0C;&#x4F46;&#x662F;&#x4ED6;&#x4EEC;&#x8FD8;&#x662F;&#x5B58;&#x5728;&#x4E00;&#x4E9B;&#x533A;&#x522B;&#x7684;&#xFF1A;</p>
<ol>
<li>CountDownLatch&#x7684;&#x4F5C;&#x7528;&#x662F;&#x5141;&#x8BB8;1&#x6216;N&#x4E2A;&#x7EBF;&#x7A0B;&#x7B49;&#x5F85;&#x5176;&#x4ED6;&#x7EBF;&#x7A0B;&#x5B8C;&#x6210;&#x6267;&#x884C;&#xFF1B;&#x800C;CyclicBarrier&#x5219;&#x662F;&#x5141;&#x8BB8;N&#x4E2A;&#x7EBF;&#x7A0B;&#x76F8;&#x4E92;&#x7B49;&#x5F85;</li>
<li>CountDownLatch&#x7684;&#x8BA1;&#x6570;&#x5668;&#x65E0;&#x6CD5;&#x88AB;&#x91CD;&#x7F6E;&#xFF1B;CyclicBarrier&#x7684;&#x8BA1;&#x6570;&#x5668;&#x53EF;&#x4EE5;&#x88AB;&#x91CD;&#x7F6E;&#x540E;&#x4F7F;&#x7528;&#xFF0C;&#x56E0;&#x6B64;&#x5B83;&#x88AB;&#x79F0;&#x4E3A;&#x662F;&#x5FAA;&#x73AF;&#x7684;barrier</li>
</ol>
<h3 id="&#x5B9E;&#x73B0;&#x5206;&#x6790;_1"><a name="&#x5B9E;&#x73B0;&#x5206;&#x6790;_1" class="anchor-navigation-ex-anchor" href="#&#x5B9E;&#x73B0;&#x5206;&#x6790;_1"><i class="fa fa-link" aria-hidden="true"></i></a><a name="&#x5B9E;&#x73B0;&#x5206;&#x6790;" class="plugin-anchor" href="#&#x5B9E;&#x73B0;&#x5206;&#x6790;"><i class="fa fa-link" aria-hidden="true"></i></a>&#x5B9E;&#x73B0;&#x5206;&#x6790;</h3>
<p>CountDownLatch&#x7ED3;&#x6784;&#x5982;&#x4E0B;</p>
<p><a href="https://gitee.com/chenssy/blog-home/raw/master/image/sijava/2018120818002.png" target="_blank"><img src="https://gitee.com/chenssy/blog-home/raw/master/image/sijava/2018120818002.png" alt="img"></a></p>
<p>&#x901A;&#x8FC7;&#x4E0A;&#x9762;&#x7684;&#x7ED3;&#x6784;&#x56FE;&#x6211;&#x4EEC;&#x53EF;&#x4EE5;&#x770B;&#x5230;&#xFF0C;CountDownLatch&#x5185;&#x90E8;&#x4F9D;&#x8D56;Sync&#x5B9E;&#x73B0;&#xFF0C;&#x800C;Sync&#x7EE7;&#x627F;AQS&#x3002;CountDownLatch&#x4EC5;&#x63D0;&#x4F9B;&#x4E86;&#x4E00;&#x4E2A;&#x6784;&#x9020;&#x65B9;&#x6CD5;&#xFF1A;</p>
<p>CountDownLatch(int count) &#xFF1A; &#x6784;&#x9020;&#x4E00;&#x4E2A;&#x7528;&#x7ED9;&#x5B9A;&#x8BA1;&#x6570;&#x521D;&#x59CB;&#x5316;&#x7684; CountDownLatch</p>
<pre><code>    public CountDownLatch(int count) {
        if (count &lt; 0) throw new IllegalArgumentException(&quot;count &lt; 0&quot;);
        this.sync = new Sync(count);
    }
</code></pre><p>sync&#x4E3A;CountDownLatch&#x7684;&#x4E00;&#x4E2A;&#x5185;&#x90E8;&#x7C7B;&#xFF0C;&#x5176;&#x5B9A;&#x4E49;&#x5982;&#x4E0B;&#xFF1A;</p>
<pre><code>    private static final class Sync extends AbstractQueuedSynchronizer {
        private static final long serialVersionUID = 4982264981922014374L;

        Sync(int count) {
            setState(count);
        }

        //&#x83B7;&#x53D6;&#x540C;&#x6B65;&#x72B6;&#x6001;
        int getCount() {
            return getState();
        }

        //&#x83B7;&#x53D6;&#x540C;&#x6B65;&#x72B6;&#x6001;
        protected int tryAcquireShared(int acquires) {
            return (getState() == 0) ? 1 : -1;
        }

        //&#x91CA;&#x653E;&#x540C;&#x6B65;&#x72B6;&#x6001;
        protected boolean tryReleaseShared(int releases) {
            for (;;) {
                int c = getState();
                if (c == 0)
                    return false;
                int nextc = c-1;
                if (compareAndSetState(c, nextc))
                    return nextc == 0;
            }
        }
    }
</code></pre><p>&#x901A;&#x8FC7;&#x8FD9;&#x4E2A;&#x5185;&#x90E8;&#x7C7B;Sync&#x6211;&#x4EEC;&#x53EF;&#x4EE5;&#x6E05;&#x695A;&#x5730;&#x770B;&#x5230;CountDownLatch&#x662F;&#x91C7;&#x7528;&#x5171;&#x4EAB;&#x9501;&#x6765;&#x5B9E;&#x73B0;&#x7684;&#x3002;</p>
<h3 id="await"><a name="await" class="anchor-navigation-ex-anchor" href="#await"><i class="fa fa-link" aria-hidden="true"></i></a><a name="await" class="plugin-anchor" href="#await"><i class="fa fa-link" aria-hidden="true"></i></a>await()</h3>
<p>CountDownLatch&#x63D0;&#x4F9B;await()&#x65B9;&#x6CD5;&#x6765;&#x4F7F;&#x5F53;&#x524D;&#x7EBF;&#x7A0B;&#x5728;&#x9501;&#x5B58;&#x5668;&#x5012;&#x8BA1;&#x6570;&#x81F3;&#x96F6;&#x4E4B;&#x524D;&#x4E00;&#x76F4;&#x7B49;&#x5F85;&#xFF0C;&#x9664;&#x975E;&#x7EBF;&#x7A0B;&#x88AB;&#x4E2D;&#x65AD;&#xFF0C;&#x5B9A;&#x4E49;&#x5982;&#x4E0B;&#xFF1A;</p>
<pre><code>    public void await() throws InterruptedException {
        sync.acquireSharedInterruptibly(1);
    }
</code></pre><p>await&#x5176;&#x5185;&#x90E8;&#x4F7F;&#x7528;AQS&#x7684;acquireSharedInterruptibly(int arg)&#xFF1A;</p>
<pre><code>    public final void acquireSharedInterruptibly(int arg)
            throws InterruptedException {
        if (Thread.interrupted())
            throw new InterruptedException();
        if (tryAcquireShared(arg) &lt; 0)
            doAcquireSharedInterruptibly(arg);
    }
</code></pre><p>&#x5728;&#x5185;&#x90E8;&#x7C7B;Sync&#x4E2D;&#x91CD;&#x5199;&#x4E86;tryAcquireShared(int arg)&#x65B9;&#x6CD5;&#xFF1A;</p>
<pre><code>        protected int tryAcquireShared(int acquires) {
            return (getState() == 0) ? 1 : -1;
        }
</code></pre><p>getState()&#x83B7;&#x53D6;&#x540C;&#x6B65;&#x72B6;&#x6001;&#xFF0C;&#x5176;&#x503C;&#x7B49;&#x4E8E;&#x8BA1;&#x6570;&#x5668;&#x7684;&#x503C;&#xFF0C;&#x4ECE;&#x8FD9;&#x91CC;&#x6211;&#x4EEC;&#x53EF;&#x4EE5;&#x770B;&#x5230;&#x5982;&#x679C;&#x8BA1;&#x6570;&#x5668;&#x503C;&#x4E0D;&#x7B49;&#x4E8E;0&#xFF0C;&#x5219;&#x4F1A;&#x8C03;&#x7528;doAcquireSharedInterruptibly(int arg)&#xFF0C;&#x8BE5;&#x65B9;&#x6CD5;&#x4E3A;&#x4E00;&#x4E2A;&#x81EA;&#x65CB;&#x65B9;&#x6CD5;&#x4F1A;&#x5C1D;&#x8BD5;&#x4E00;&#x76F4;&#x53BB;&#x83B7;&#x53D6;&#x540C;&#x6B65;&#x72B6;&#x6001;&#xFF1A;</p>
<pre><code>    private void doAcquireSharedInterruptibly(int arg)
            throws InterruptedException {
        final Node node = addWaiter(Node.SHARED);
        boolean failed = true;
        try {
            for (;;) {
                final Node p = node.predecessor();
                if (p == head) {
                    /**
                     * &#x5BF9;&#x4E8E;CountDownLatch&#x800C;&#x8A00;&#xFF0C;&#x5982;&#x679C;&#x8BA1;&#x6570;&#x5668;&#x503C;&#x4E0D;&#x7B49;&#x4E8E;0&#xFF0C;&#x90A3;&#x4E48;r &#x4F1A;&#x4E00;&#x76F4;&#x5C0F;&#x4E8E;0
                     */
                    int r = tryAcquireShared(arg);
                    if (r &gt;= 0) {
                        setHeadAndPropagate(node, r);
                        p.next = null; // help GC
                        failed = false;
                        return;
                    }
                }
                //&#x7B49;&#x5F85;
                if (shouldParkAfterFailedAcquire(p, node) &amp;&amp;
                        parkAndCheckInterrupt())
                    throw new InterruptedException();
            }
        } finally {
            if (failed)
                cancelAcquire(node);
        }
    }
</code></pre><h3 id="countdown"><a name="countdown" class="anchor-navigation-ex-anchor" href="#countdown"><i class="fa fa-link" aria-hidden="true"></i></a><a name="countdown" class="plugin-anchor" href="#countdown"><i class="fa fa-link" aria-hidden="true"></i></a>countDown()</h3>
<p>CountDownLatch&#x63D0;&#x4F9B;countDown() &#x65B9;&#x6CD5;&#x9012;&#x51CF;&#x9501;&#x5B58;&#x5668;&#x7684;&#x8BA1;&#x6570;&#xFF0C;&#x5982;&#x679C;&#x8BA1;&#x6570;&#x5230;&#x8FBE;&#x96F6;&#xFF0C;&#x5219;&#x91CA;&#x653E;&#x6240;&#x6709;&#x7B49;&#x5F85;&#x7684;&#x7EBF;&#x7A0B;&#x3002;</p>
<pre><code>    public void countDown() {
        sync.releaseShared(1);
    }
</code></pre><p>&#x5185;&#x90E8;&#x8C03;&#x7528;AQS&#x7684;releaseShared(int arg)&#x65B9;&#x6CD5;&#x6765;&#x91CA;&#x653E;&#x5171;&#x4EAB;&#x9501;&#x540C;&#x6B65;&#x72B6;&#x6001;&#xFF1A;</p>
<pre><code>    public final boolean releaseShared(int arg) {
        if (tryReleaseShared(arg)) {
            doReleaseShared();
            return true;
        }
        return false;
    }
</code></pre><p>tryReleaseShared(int arg)&#x65B9;&#x6CD5;&#x88AB;CountDownLatch&#x7684;&#x5185;&#x90E8;&#x7C7B;Sync&#x91CD;&#x5199;&#xFF1A;</p>
<pre><code>    protected boolean tryReleaseShared(int releases) {
        for (;;) {
            //&#x83B7;&#x53D6;&#x9501;&#x72B6;&#x6001;
            int c = getState();
            //c == 0 &#x76F4;&#x63A5;&#x8FD4;&#x56DE;&#xFF0C;&#x91CA;&#x653E;&#x9501;&#x6210;&#x529F;
            if (c == 0)
                return false;
            //&#x8BA1;&#x7B97;&#x65B0;&#x201C;&#x9501;&#x8BA1;&#x6570;&#x5668;&#x201D;
            int nextc = c-1;
            //&#x66F4;&#x65B0;&#x9501;&#x72B6;&#x6001;&#xFF08;&#x8BA1;&#x6570;&#x5668;&#xFF09;
            if (compareAndSetState(c, nextc))
                return nextc == 0;
        }
    }
</code></pre><h3 id="&#x603B;&#x7ED3;_1"><a name="&#x603B;&#x7ED3;_1" class="anchor-navigation-ex-anchor" href="#&#x603B;&#x7ED3;_1"><i class="fa fa-link" aria-hidden="true"></i></a><a name="&#x603B;&#x7ED3;" class="plugin-anchor" href="#&#x603B;&#x7ED3;"><i class="fa fa-link" aria-hidden="true"></i></a>&#x603B;&#x7ED3;</h3>
<p>CountDownLatch&#x5185;&#x90E8;&#x901A;&#x8FC7;&#x5171;&#x4EAB;&#x9501;&#x5B9E;&#x73B0;&#x3002;&#x5728;&#x521B;&#x5EFA;CountDownLatch&#x5B9E;&#x4F8B;&#x65F6;&#xFF0C;&#x9700;&#x8981;&#x4F20;&#x9012;&#x4E00;&#x4E2A;int&#x578B;&#x7684;&#x53C2;&#x6570;&#xFF1A;count&#xFF0C;&#x8BE5;&#x53C2;&#x6570;&#x4E3A;&#x8BA1;&#x6570;&#x5668;&#x7684;&#x521D;&#x59CB;&#x503C;&#xFF0C;&#x4E5F;&#x53EF;&#x4EE5;&#x7406;&#x89E3;&#x4E3A;&#x8BE5;&#x5171;&#x4EAB;&#x9501;&#x53EF;&#x4EE5;&#x83B7;&#x53D6;&#x7684;&#x603B;&#x6B21;&#x6570;&#x3002;&#x5F53;&#x67D0;&#x4E2A;&#x7EBF;&#x7A0B;&#x8C03;&#x7528;await()&#x65B9;&#x6CD5;&#xFF0C;&#x7A0B;&#x5E8F;&#x9996;&#x5148;&#x5224;&#x65AD;count&#x7684;&#x503C;&#x662F;&#x5426;&#x4E3A;0&#xFF0C;&#x5982;&#x679C;&#x4E0D;&#x4F1A;0&#x7684;&#x8BDD;&#x5219;&#x4F1A;&#x4E00;&#x76F4;&#x7B49;&#x5F85;&#x76F4;&#x5230;&#x4E3A;0&#x4E3A;&#x6B62;&#x3002;&#x5F53;&#x5176;&#x4ED6;&#x7EBF;&#x7A0B;&#x8C03;&#x7528;countDown()&#x65B9;&#x6CD5;&#x65F6;&#xFF0C;&#x5219;&#x6267;&#x884C;&#x91CA;&#x653E;&#x5171;&#x4EAB;&#x9501;&#x72B6;&#x6001;&#xFF0C;&#x4F7F;count&#x503C; - 1&#x3002;&#x5F53;&#x5728;&#x521B;&#x5EFA;CountDownLatch&#x65F6;&#x521D;&#x59CB;&#x5316;&#x7684;count&#x53C2;&#x6570;&#xFF0C;&#x5FC5;&#x987B;&#x8981;&#x6709;count&#x7EBF;&#x7A0B;&#x8C03;&#x7528;countDown&#x65B9;&#x6CD5;&#x624D;&#x4F1A;&#x4F7F;&#x8BA1;&#x6570;&#x5668;count&#x7B49;&#x4E8E;0&#xFF0C;&#x9501;&#x624D;&#x4F1A;&#x91CA;&#x653E;&#xFF0C;&#x524D;&#x9762;&#x7B49;&#x5F85;&#x7684;&#x7EBF;&#x7A0B;&#x624D;&#x4F1A;&#x7EE7;&#x7EED;&#x8FD0;&#x884C;&#x3002;&#x6CE8;&#x610F;CountDownLatch&#x4E0D;&#x80FD;&#x56DE;&#x6EDA;&#x91CD;&#x7F6E;&#x3002;</p>
<h3 id="&#x5E94;&#x7528;&#x793A;&#x4F8B;_1"><a name="&#x5E94;&#x7528;&#x793A;&#x4F8B;_1" class="anchor-navigation-ex-anchor" href="#&#x5E94;&#x7528;&#x793A;&#x4F8B;_1"><i class="fa fa-link" aria-hidden="true"></i></a><a name="&#x5E94;&#x7528;&#x793A;&#x4F8B;" class="plugin-anchor" href="#&#x5E94;&#x7528;&#x793A;&#x4F8B;"><i class="fa fa-link" aria-hidden="true"></i></a>&#x5E94;&#x7528;&#x793A;&#x4F8B;</h3>
<p>&#x793A;&#x4F8B;&#x4ECD;&#x7136;&#x4F7F;&#x7528;&#x5F00;&#x4F1A;&#x6848;&#x4F8B;&#x3002;&#x8001;&#x677F;&#x8FDB;&#x5165;&#x4F1A;&#x8BAE;&#x5BA4;&#x7B49;&#x5F85;5&#x4E2A;&#x4EBA;&#x5168;&#x90E8;&#x5230;&#x8FBE;&#x4F1A;&#x8BAE;&#x5BA4;&#x624D;&#x4F1A;&#x5F00;&#x4F1A;&#x3002;&#x6240;&#x4EE5;&#x8FD9;&#x91CC;&#x6709;&#x4E24;&#x4E2A;&#x7EBF;&#x7A0B;&#x8001;&#x677F;&#x7B49;&#x5F85;&#x5F00;&#x4F1A;&#x7EBF;&#x7A0B;&#x3001;&#x5458;&#x5DE5;&#x5230;&#x8FBE;&#x4F1A;&#x8BAE;&#x5BA4;&#xFF1A;</p>
<pre><code>public class CountDownLatchTest {
    private static CountDownLatch countDownLatch = new CountDownLatch(5);

    /**
     * Boss&#x7EBF;&#x7A0B;&#xFF0C;&#x7B49;&#x5F85;&#x5458;&#x5DE5;&#x5230;&#x8FBE;&#x5F00;&#x4F1A;
     */
    static class BossThread extends Thread{
        @Override
        public void run() {
            System.out.println(&quot;Boss&#x5728;&#x4F1A;&#x8BAE;&#x5BA4;&#x7B49;&#x5F85;&#xFF0C;&#x603B;&#x5171;&#x6709;&quot; + countDownLatch.getCount() + &quot;&#x4E2A;&#x4EBA;&#x5F00;&#x4F1A;...&quot;);
            try {
                //Boss&#x7B49;&#x5F85;
                countDownLatch.await();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }

            System.out.println(&quot;&#x6240;&#x6709;&#x4EBA;&#x90FD;&#x5DF2;&#x7ECF;&#x5230;&#x9F50;&#x4E86;&#xFF0C;&#x5F00;&#x4F1A;&#x5427;...&quot;);
        }
    }

    //&#x5458;&#x5DE5;&#x5230;&#x8FBE;&#x4F1A;&#x8BAE;&#x5BA4;
    static class EmpleoyeeThread  extends Thread{
        @Override
        public void run() {
            System.out.println(Thread.currentThread().getName() + &quot;&#xFF0C;&#x5230;&#x8FBE;&#x4F1A;&#x8BAE;&#x5BA4;....&quot;);
            //&#x5458;&#x5DE5;&#x5230;&#x8FBE;&#x4F1A;&#x8BAE;&#x5BA4; count - 1
            countDownLatch.countDown();
        }
    }

    public static void main(String[] args){
        //Boss&#x7EBF;&#x7A0B;&#x542F;&#x52A8;
        new BossThread().start();

        for(int i = 0,j = countDownLatch.getCount() ; i &lt; j ; i++){
            new EmpleoyeeThread().start();
        }
    }
}
</code></pre><p>&#x8FD0;&#x884C;&#x7ED3;&#x679C;&#xFF1A;</p>
<p><img src="https://gitee.com/zisuu/picture/raw/master/img/20201216213403.png" alt="image-20201216213403620"></p>

<script>console.log("plugin-popup....");document.onclick = function(e){ e.target.tagName === "IMG" && window.open(e.target.src,e.target.src)}</script><style>img{cursor:pointer}</style>
                                
                                </section>
                            
    </div>
    <div class="search-results">
        <div class="has-results">
            
            <h1 class="search-results-title"><span class='search-results-count'></span> results matching "<span class='search-query'></span>"</h1>
            <ul class="search-results-list"></ul>
            
        </div>
        <div class="no-results">
            
            <h1 class="search-results-title">No results matching "<span class='search-query'></span>"</h1>
            
        </div>
    </div>
</div>

                        </div>
                    </div>
                
            </div>

            
                
                <a href="1.AQS.html" class="navigation navigation-prev " aria-label="Previous page: 8.AQS.md">
                    <i class="fa fa-angle-left"></i>
                </a>
                
                
                <a href="../blockingQueue/1.阻塞队列.html" class="navigation navigation-next " aria-label="Next page: 10.阻塞队列.md">
                    <i class="fa fa-angle-right"></i>
                </a>
                
            
        
    </div>

    <script>
        var gitbook = gitbook || [];
        gitbook.push(function() {
            gitbook.page.hasChanged({"page":{"title":"9.基于AQS的并发工具.md","level":"6.10","depth":1,"next":{"title":"10.阻塞队列.md","level":"6.11","depth":1,"path":"多线程/blockingQueue/1.阻塞队列.md","ref":"多线程/blockingQueue/1.阻塞队列.md","articles":[]},"previous":{"title":"8.AQS.md","level":"6.9","depth":1,"path":"多线程/aqs/1.AQS.md","ref":"多线程/aqs/1.AQS.md","articles":[]},"dir":"ltr"},"config":{"plugins":["-sharing","splitter","expandable-chapters-small","anchors","sharing-plus","anchor-navigation-ex","favicon","expandable-chapters","toggle-chapters","back-to-top-button","code","-lunr","-search","search-pro","pageview-count","popup","livereload"],"styles":{"website":"./styles/website.css"},"pluginsConfig":{"livereload":{},"splitter":{},"search-pro":{},"sharing-plus":{"qq":false,"all":["facebook","google","twitter","instapaper","linkedin","pocket","stumbleupon"],"douban":false,"facebook":true,"weibo":false,"instapaper":false,"whatsapp":false,"hatenaBookmark":false,"twitter":true,"messenger":false,"line":false,"vk":false,"pocket":true,"google":false,"viber":false,"stumbleupon":false,"qzone":false,"linkedin":false},"popup":{},"code":{"copyButtons":true},"fontsettings":{"theme":"white","family":"sans","size":2},"highlight":{},"anchor-navigation-ex":{"associatedWithSummary":true,"float":{"floatIcon":"fa fa-navicon","level1Icon":"","level2Icon":"","level3Icon":"","showLevelIcon":false},"mode":"float","multipleH1":true,"pageTop":{"level1Icon":"","level2Icon":"","level3Icon":"","showLevelIcon":false},"printLog":false,"showGoTop":true,"showLevel":false},"favicon":{},"back-to-top-button":{},"pageview-count":{},"expandable-chapters-small":{},"sharing":{"qq":false,"all":["google","facebook","weibo","twitter","qq","qzone","linkedin","pocket"],"douban":false,"facebook":false,"weibo":false,"instapaper":false,"whatsapp":false,"hatenaBookmark":false,"twitter":false,"messenger":false,"line":false,"vk":false,"pocket":false,"google":false,"viber":false,"stumbleupon":false,"qzone":false,"linkedin":false},"theme-default":{"styles":{"website":"styles/website.css","pdf":"styles/pdf.css","epub":"styles/epub.css","mobi":"styles/mobi.css","ebook":"styles/ebook.css","print":"styles/print.css"},"showLevel":false},"anchors":{},"toggle-chapters":{},"expandable-chapters":{}},"theme":"default","author":"hzh","pdf":{"pageNumbers":true,"fontSize":12,"fontFamily":"Arial","paperSize":"a4","chapterMark":"pagebreak","pageBreaksBefore":"/","margin":{"right":62,"left":62,"top":56,"bottom":56}},"structure":{"langs":"LANGS.md","readme":"README.md","glossary":"GLOSSARY.md","summary":"SUMMARY.md"},"variables":{},"title":"计算机 ,java , 大数据 基础知识体系","language":"zh-hans","gitbook":"3.2.3"},"file":{"path":"多线程/aqs/2.基于AQS的并发工具.md","mtime":"2020-12-20T01:18:36.486Z","type":"markdown"},"gitbook":{"version":"3.2.3","time":"2020-12-30T11:12:56.611Z"},"basePath":"../..","book":{"language":""}});
        });
    </script>
</div>

        
    <script src="../../gitbook/gitbook.js"></script>
    <script src="../../gitbook/theme.js"></script>
    
        
        <script src="../../gitbook/gitbook-plugin-splitter/splitter.js"></script>
        
    
        
        <script src="../../gitbook/gitbook-plugin-expandable-chapters-small/expandable-chapters-small.js"></script>
        
    
        
        <script src="../../gitbook/gitbook-plugin-sharing-plus/buttons.js"></script>
        
    
        
        <script src="../../gitbook/gitbook-plugin-expandable-chapters/expandable-chapters.js"></script>
        
    
        
        <script src="../../gitbook/gitbook-plugin-toggle-chapters/toggle.js"></script>
        
    
        
        <script src="../../gitbook/gitbook-plugin-back-to-top-button/plugin.js"></script>
        
    
        
        <script src="../../gitbook/gitbook-plugin-code/plugin.js"></script>
        
    
        
        <script src="../../gitbook/gitbook-plugin-search-pro/jquery.mark.min.js"></script>
        
    
        
        <script src="../../gitbook/gitbook-plugin-search-pro/search.js"></script>
        
    
        
        <script src="../../gitbook/gitbook-plugin-pageview-count/plugin.js"></script>
        
    
        
        <script src="../../gitbook/gitbook-plugin-livereload/plugin.js"></script>
        
    
        
        <script src="../../gitbook/gitbook-plugin-fontsettings/fontsettings.js"></script>
        
    

    </body>
</html>

